Simulate students taking sandwiches in order and count how many students cannot eat.
Number of Students Unable to Eat Lunch
You are given two lists:
students: the preferences of students standing in a queuesandwiches: the type of sandwich at each position in the lunch stack
Each student prefers exactly one type of sandwich, represented by 0 or 1. The student at the front of the queue examines the top sandwich.
- If the student's preference matches the top sandwich, the student takes it and leaves the queue.
- Otherwise, the student goes to the back of the queue.
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.
Input Format
Input
students: an integer array of lengthn, where each value is0or1sandwiches: an integer array of lengthn, where each value is0or1
The two arrays contain the same number of elements.
Output Format
Output
- Return an integer: the number of students who are unable to eat lunch.
Constraints
students[i]andsandwiches[i]are each either0or1- The lengths of
studentsandsandwichesare equal
Example 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
Unlock deeper context for this problem
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.