#1700
Number of Students Unable to Eat Lunch
newbie · 190 · lc easy +16 · verified · 79.4% accepted · 2,707 likes · top 91%
Description
A cafeteria has circular (0) and square (1) sandwiches stacked in a pile, and students stand in a queue each preferring one type. Each step: if the student at the front wants the top sandwich they take it and leave; otherwise they move to the back of the queue. The process stops when no student in line wants the current top sandwich.
Given integer arrays students and sandwiches (index 0 is front/top), return the number of students who cannot 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
Code
1
2
3