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.The key idea in #1700 Number of Students Unable to Eat Lunch is to simulate how students pick sandwiches based on their preferences. Students stand in a queue, while sandwiches are served from a stack-like order. If the student at the front prefers the top sandwich, they take it and leave; otherwise, they move to the back of the queue.
A straightforward approach is to simulate the queue rotation using a queue data structure. Continue processing until no student in the queue wants the current sandwich. At that point, the remaining students cannot eat.
An even more efficient insight is to count the number of students preferring each sandwich type. As sandwiches are processed, decrease the corresponding count. If a sandwich type appears but no students prefer it, the process stops immediately.
This counting approach avoids repeated queue rotations and keeps the solution efficient. The overall complexity is typically O(n) time with O(1) extra space since only two preference counts are tracked.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Queue Simulation | O(n^2) worst case | O(n) |
| Counting Preference Method | O(n) | O(1) |
NeetCodeIO
Use these hints if you're stuck. Try solving on your own first.
Simulate the given in the statement
Calculate those who will eat instead of those who will not.
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.
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.
1import java.util.LinkedList;
2import java.util.Queue;
3
4public class Solution {
5 public int
The Java solution also employs a Queue for student representation. It follows a similar loop as described for C++ code, leveraging Java's queue operations to circulate around the student list and identify potential matches with the current sandwich.
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.
Time Complexity: O(n) because it processes each list one time.
Space Complexity: O(1) since it only uses fixed additional space for counting.
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, this type of problem is common in coding interviews because it tests understanding of queues, simulation, and problem simplification. It is considered an easy-level problem but checks whether candidates can identify a more efficient counting approach.
A queue is commonly used to simulate the student line, while the sandwiches behave like a stack. However, the most efficient solution avoids simulation and instead uses simple counters to track student preferences.
The process stops when the current sandwich type is not preferred by any remaining student in the queue. Since the sandwich order cannot change, no further progress is possible and all remaining students will be unable to eat.
The optimal approach counts how many students prefer each sandwich type (0 or 1). As sandwiches are processed, the corresponding count is reduced. If a sandwich appears that no remaining student prefers, the process stops and the remaining students cannot eat.
The Python solution tallies the food type preferences using a list of size 2. It uses a for loop to decrease the count of available preferences upon each sandwich distribution. If all choices are exhausted, it sums the remaining students.