Number of Students Unable to Eat Lunch — LeetCode #1700 (Easy)
Tags: Array, Stack, Queue, Simulation
Related patterns: Queue, Simulation
Brute Force approach
Time complexity: O(n²). Space complexity: O(1).
In this approach, we simulate the process of students trying to take sandwiches. Each student checks the top sandwich and either takes it or moves to the back of the queue. This continues until no student can take the sandwich on top.
The time complexity is O(n²) because in the worst case, each student may go through the queue n times, leading to n * n operations.
- Step 1: Initialize a queue with the students' preferences.
- Step 2: While there are sandwiches left, check the student at the front of the queue.
- Step 3: If the student prefers the top sandwich, remove both the student and the sandwich; otherwise, move the student to the end of the queue.
- Step 4: Repeat until no students can take the top sandwich.
Initial state: students = [1, 1, 0, 0], sandwiches = [0, 1, 0, 1]
1. Student 1 (prefers 1) sees sandwich 0 (not taken) → moves to end → students = [1, 0, 0, 1]
2. Student 1 (prefers 1) sees sandwich 0 (not taken) → moves to end → students = [0, 0, 1, 1]
3. Student 0 (prefers 0) sees sandwich 0 (taken) → takes it → students = [0, 1, 1], sandwiches = [1, 0, 1]
4. Student 0 (prefers 0) sees sandwich 1 (not taken) → moves to end → students = [1, 1, 0]
5. Student 1 (prefers 1) sees sandwich 1 (taken) → takes it → students = [1, 0], sandwiches = [0, 1]
6. Student 1 (prefers 1) sees sandwich 0 (not taken) → moves to end → students = [0, 1]
Optimal Solution approach
Time complexity: O(n). Space complexity: O(1).
Instead of simulating the entire process, we can count how many students prefer each type of sandwich. If the number of students who prefer a type of sandwich exceeds the number of available sandwiches of that type, we can directly calculate how many students will be unable to eat.
The time complexity is O(n) because we only traverse the students and sandwiches arrays once each, leading to linear time complexity.
- Step 1: Count the number of students who prefer circular (0) and square (1) sandwiches.
- Step 2: For each sandwich type in the stack, decrement the respective count of students who prefer that type.
- Step 3: If a sandwich type is exhausted, check how many students are left who prefer that type.
- Step 4: The remaining students who cannot eat are those who still prefer a sandwich type that is no longer available.
Initial state: students = [1, 1, 0, 0], sandwiches = [0, 1, 0, 1]
1. Count preferences: count = [2, 2] (2 prefer 0, 2 prefer 1)
2. Sandwich 0: count[0] becomes 1 → count = [1, 2]
3. Sandwich 1: count[1] becomes 1 → count = [1, 1]
4. Sandwich 0: count[0] becomes 0 → count = [0, 1]
5. Sandwich 1: count[1] becomes 0 → count = [0, 0]
6. No sandwiches left, return count[0] + count[1] = 0.
Key Insights
- Understanding student preferences is crucial.
- Simulating the process can be inefficient; counting preferences is more effective.
Common Mistakes
- Not considering the case where all sandwiches are taken.
- Failing to optimize the simulation approach.
Interview Tips
- Always think about edge cases, like when students have no preferences left.
- Consider counting or grouping strategies to simplify the problem.
- Practice simulating processes to understand the flow before optimizing.