Welcome to the school cafeteria lunch rush! π½οΈ
The cafeteria serves two types of sandwiches: circular sandwiches (0) and square sandwiches (1). Students are lined up in a queue, each with their own preference for either circular or square sandwiches.
Here's how the lunch process works:
- π₯ͺ Sandwiches are placed in a stack (top sandwich served first)
- π₯ Students wait in a queue (front student gets served first)
- β If the front student likes the top sandwich β they take it and leave
- β If the front student doesn't like the top sandwich β they go to the back of the queue
The process continues until no remaining students want the current top sandwich, meaning some students will go hungry! π’
Your task: Given the sandwich stack and student preferences, determine how many students will be unable to eat lunch.
Input: Two arrays - students[] (preferences in queue order) and sandwiches[] (sandwich types in stack order)
Output: Number of students who cannot get their preferred sandwich
Input & Output
Visualization
Time & Space Complexity
We make one pass to count students (O(n)) and one pass through sandwiches (O(n)), giving us O(n) total.
We only need two counters for the two types of sandwiches, regardless of input size.
Constraints
- 1 β€ students.length, sandwiches.length β€ 100
- students.length == sandwiches.length
- students[i] is 0 or 1, where 0 means circular sandwich preference
- sandwiches[i] is 0 or 1, where 1 means square sandwich