The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers 0 and 1 respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.
The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step:
This continues until none of the queue students want to take the top sandwich and are thus unable to eat.
You are given two integer arrays students and sandwiches where sandwiches[i] is the type of the ith sandwich in the stack (i = 0 is the top of the stack) and students[j] is the preference of the jth student in the initial queue (j = 0 is the front of the queue). Return the number of students that are unable to eat.
Example 1:
Input: students = [1,1,0,0], sandwiches = [0,1,0,1] Output: 0 Explanation: - Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1]. - Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1]. - Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1]. - Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0]. - Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1]. - Front student leaves the top sandwich and returns to the end of the line making students = [0,1]. - Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1]. - Front student takes the top sandwich and leaves the line making students = [] and sandwiches = []. Hence all students are able to eat.
Example 2:
Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1] Output: 3
Constraints:
1 <= students.length, sandwiches.length <= 100students.length == sandwiches.lengthsandwiches[i] is 0 or 1.students[i] is 0 or 1.In this approach, we simulate the exact process of distributing sandwiches using a queue-like structure for the students. We repeatedly check if the student at the front of the queue can take the sandwich at the top of the stack. If not, the student is moved to the back of the queue. This continues until we complete a full cycle without any students taking a sandwich, indicating they can't eat.
This C code defines a function that uses a circular iteration over the student preferences to check each against the current sandwich on the stack. We use two indices: one for students and one for sandwiches, plus a cycle counter to detect when all options are exhausted without making progress. The cycle count control helps to stop the loop when no further matches are possible, making the method more efficient.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2) in the worst scenario because each student may be checked multiple times.
Space Complexity: O(1) since it only uses a fixed amount of extra space.
This approach uses counting to determine if students can satisfy their preferences before iterating over sandwiches. By counting the number of students preferring each type, we can efficiently determine how many students will be unable to eat if sandwiches of their preference run out before they can eat.
The C implementation counts the preferences for circular and square sandwiches. It iterates over the sandwiches and decreases the count of the suitable preference whenever a sandwich is taken. If no more students prefer the top sandwich, it returns the remaining count.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) because it processes each list one time.
Space Complexity: O(1) since it only uses fixed additional space for counting.
| Approach | Complexity |
|---|---|
| Simulate the Process Using a Queue | Time Complexity: O(n^2) in the worst scenario because each student may be checked multiple times. |
| Count Preferences and Match with Sandwiches | Time Complexity: O(n) because it processes each list one time. |
Number of Students Unable to Eat Lunch - Leetcode 1700 - Python • NeetCodeIO • 22,926 views views
Watch 9 more video solutions →Practice Number of Students Unable to Eat Lunch with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor