Simulate students taking sandwiches in order and count how many students cannot eat.
You are given two lists:
students: the preferences of students standing in a queuesandwiches: the type of sandwich at each position in the lunch stackEach student prefers exactly one type of sandwich, represented by 0 or 1. The student at the front of the queue examines the top sandwich.
This process continues until the student at the front of the queue does not want the top sandwich, and no student in the queue wants that sandwich either. At that point, the remaining students are unable to eat.
Return the number of students who cannot eat lunch.
students: an integer array of length n, where each value is 0 or 1sandwiches: an integer array of length n, where each value is 0 or 1The two arrays contain the same number of elements.
students[i] and sandwiches[i] are each either 0 or 1students and sandwiches are equalExample 1
Input
students = [1,1,0,0] sandwiches = [0,1,0,1]
Output
0
Explanation
The first student moves to the back, then the top sandwich 0 is taken by a student who wants 0. Eventually all students get a sandwich.
Example 2
Input
students = [1,1,1,0,0,1] sandwiches = [1,0,0,0,1,1]
Output
3
Explanation
After some students rotate, the remaining students all want 1 but the next sandwich is 0. No one wants it, so 3 students cannot eat.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.