Problem Statement
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:
- If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue.
- Otherwise, they will leave it and go to the queue's end.
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
Try here before watching the video.
Video Solution
Java Code
class Solution {
public int countStudents(int[] students, int[] sandwiches) {
int len = students.length;
Queue<Integer> studentQueue = new LinkedList<>();
Stack<Integer> sandwichStack = new Stack<>();
for (int i = 0; i < len; i++) {
sandwichStack.push(sandwiches[len - i - 1]);
studentQueue.offer(students[i]);
}
int lastServed = 0;
while (studentQueue.size() > 0 && lastServed < studentQueue.size()) {
if (sandwichStack.peek() == studentQueue.peek()) {
sandwichStack.pop();
studentQueue.poll();
lastServed = 0;
} else {
studentQueue.offer(studentQueue.poll());
lastServed++;
}
}
return studentQueue.size();
}
}
C++ Code
class Solution {
public:
int countStudents(vector<int>& students, vector<int>& sandwiches) {
int len = students.size();
queue<int> studentQueue;
stack<int> sandwichStack;
for (int i = 0; i < len; i++) {
sandwichStack.push(sandwiches[len - i - 1]);
studentQueue.push(students[i]);
}
int lastServed = 0;
while (!studentQueue.empty() && lastServed < studentQueue.size()) {
if (sandwichStack.top() == studentQueue.front()) {
sandwichStack.pop();
studentQueue.pop();
lastServed = 0;
} else {
studentQueue.push(studentQueue.front());
studentQueue.pop();
lastServed++;
}
}
return studentQueue.size();
}
};
Python Code
class Solution:
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
studentQueue = collections.deque(students)
sandwichStack = sandwiches[::-1]
lastServed = 0
while studentQueue and lastServed < len(studentQueue):
if sandwichStack[-1] == studentQueue[0]:
sandwichStack.pop()
studentQueue.popleft()
lastServed = 0
else:
studentQueue.append(studentQueue.popleft())
lastServed += 1
return len(studentQueue)
Javascript Code
var countStudents = function(students, sandwiches) {
const studentQueue = students.slice();
const sandwichStack = sandwiches.slice().reverse();
let lastServed = 0;
while (studentQueue.length > 0 && lastServed < studentQueue.length) {
if (sandwichStack[sandwichStack.length - 1] === studentQueue[0]) {
sandwichStack.pop();
studentQueue.shift();
lastServed = 0;
} else {
studentQueue.push(studentQueue.shift());
lastServed++;
}
}
return studentQueue.length;
};
Go Code
func countStudents(students []int, sandwiches []int) int {
studentQueue := make([]int, len(students))
copy(studentQueue, students)
sandwichStack := make([]int, len(sandwiches))
copy(sandwichStack, sandwiches)
reverse(sandwichStack)
lastServed := 0
for len(studentQueue) > 0 && lastServed < len(studentQueue) {
if sandwichStack[len(sandwichStack)-1] == studentQueue[0] {
sandwichStack = sandwichStack[:len(sandwichStack)-1]
studentQueue = studentQueue[1:]
lastServed = 0
} else {
studentQueue = append(studentQueue[1:], studentQueue[0])
lastServed++
}
}
return len(studentQueue)
}
func reverse(arr []int) {
for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
arr[i], arr[j] = arr[j], arr[i]
}
}