Number of Students Unable to Eat Lunch (LeetCode 1700): Solution and Explanation
Students in a queue take the top sandwich only if it is their preferred shape, otherwise they move to the back. Return how many students never eat. For students = [1,1,1,0,0,1] and sandwiches = [1,0,0,0,1,1] the answer is 3.
Intuition
Simulating the queue is allowed, but it hides the real rule. A student who does not want the top sandwich just goes to the back, so the line keeps cycling.
If anyone left in line wants the top sandwich, the cycling will eventually bring them to the front and they will take it. Where they stand does not matter, only whether they exist.
So the whole queue reduces to two numbers: how many students want circular and how many want square. Serve sandwiches from the top while the matching count is positive. The first sandwich with a count of zero blocks the stack for good.
Approach: Count preferences
- Count students who want shape 0 and shape 1 into
count[0]andcount[1]. - Walk
sandwichesfrom index 0 (the top). - If
count[sandwich]is 0, nobody can take it and nobody behind it is reachable, so return the number of sandwiches left,n - i. - Otherwise decrement
count[sandwich]. If the loop finishes, everyone ate: return 0.
Step-by-step example
students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
- Count: 2 students want circular (0), 4 want square (1).
- Sandwich 0 is square: count[1] = 4 > 0, a square lover takes it. count[1] = 3.
- Sandwich 1 is circular: count[0] = 2 > 0. count[0] = 1.
- Sandwich 2 is circular: count[0] = 1 > 0. count[0] = 0.
- Sandwich 3 is circular but count[0] = 0. Nobody wants it, so the 6 - 3 = 3 remaining students cannot eat. Answer 3.
Code: Python, Java, C++
1class Solution:
2 def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
3 count = [0, 0]
4 for s in students:
5 count[s] += 1
6 for i, sandwich in enumerate(sandwiches):
7 if count[sandwich] == 0:
8 return len(sandwiches) - i
9 count[sandwich] -= 1
10 return 01class Solution {
2 public int countStudents(int[] students, int[] sandwiches) {
3 int[] count = new int[2];
4 for (int s : students) {
5 count[s]++;
6 }
7 for (int i = 0; i < sandwiches.length; i++) {
8 if (count[sandwiches[i]] == 0) {
9 return sandwiches.length - i;
10 }
11 count[sandwiches[i]]--;
12 }
13 return 0;
14 }
15}1class Solution {
2public:
3 int countStudents(vector<int>& students, vector<int>& sandwiches) {
4 int count[2] = {0, 0};
5 for (int s : students) {
6 count[s]++;
7 }
8 for (int i = 0; i < (int)sandwiches.size(); i++) {
9 if (count[sandwiches[i]] == 0) {
10 return (int)sandwiches.size() - i;
11 }
12 count[sandwiches[i]]--;
13 }
14 return 0;
15 }
16};Time and space complexity
O(n)
One pass to count preferences and one pass over the sandwiches.
O(1)
Only two counters are stored.
Other approaches
Direct queue simulation (time O(n^2), space O(n))
Use a real queue. Move the front student to the back when they refuse, and stop when a full cycle of the queue passes without anyone eating. Correct but each sandwich can cost a full rotation.
Common mistakes
- Stopping the simulation after one refusal instead of after a full rotation with no takers.
- Returning the number of remaining students who dislike the top sandwich. Every remaining student is stuck, including those who would like a sandwich further down.
- Reading
sandwichesfrom the end. Index 0 is the top of the stack.
Frequently asked questions
Why can we ignore the order of the students?
The front student either eats or moves to the back, so the queue keeps cycling. Any student who wants the top sandwich reaches the front within one cycle. Order only changes how long it takes, not who eats.
Why does one unwanted sandwich stop everything?
It stays on top forever because nobody will take it, and the sandwiches under it can never be reached. So every student still in line goes hungry.
Where is the stack in this problem?
The sandwiches form the stack and the students form the queue. The counting trick shows that only the stack order matters, because the queue order washes out.