Skip to content

Commit 4541c2c

Browse files
Create 1700.NumberofStudentsUnabletoEatLunch.py
1 parent feca078 commit 4541c2c

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'''
2+
The school cafeteria offers circular and square sandwiches
3+
at lunch break, referred to by numbers 0 and 1 respectively.
4+
All students stand in a queue. Each student either prefers
5+
square or circular sandwiches.
6+
7+
The number of sandwiches in the cafeteria is equal to the
8+
number of students. The sandwiches are placed in a stack.
9+
At each step:
10+
- If the student at the front of the queue prefers
11+
the sandwich on the top of the stack, they will
12+
take it and leave the queue.
13+
- Otherwise, they will leave it and go to the queue's
14+
end.
15+
16+
This continues until none of the queue students want to
17+
take the top sandwich and are thus unable to eat.
18+
19+
You are given two integer arrays students and sandwiches
20+
where sandwiches[i] is the type of the i​​​​​​th sandwich in
21+
the stack (i = 0 is the top of the stack) and students[j]
22+
is the preference of the j​​​​​​th student in the initial queue
23+
(j = 0 is the front of the queue). Return the number of
24+
students that are unable to eat.
25+
26+
Example:
27+
Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
28+
Output: 0
29+
Explanation:
30+
- Front student leaves the top sandwich and
31+
returns to the end of the line making
32+
students = [1,0,0,1].
33+
- Front student leaves the top sandwich and
34+
returns to the end of the line making
35+
students = [0,0,1,1].
36+
- Front student takes the top sandwich and
37+
leaves the line making students = [0,1,1]
38+
and sandwiches = [1,0,1].
39+
- Front student leaves the top sandwich and
40+
eturns to the end of the line making
41+
students = [1,1,0].
42+
- Front student takes the top sandwich and
43+
leaves the line making students = [1,0]
44+
and sandwiches = [0,1].
45+
- Front student leaves the top sandwich and
46+
returns to the end of the line making
47+
students = [0,1].
48+
- Front student takes the top sandwich and
49+
leaves the line making students = [1] and
50+
sandwiches = [1].
51+
- Front student takes the top sandwich and
52+
leaves the line making students = [] and
53+
sandwiches = [].
54+
Hence all students are able to eat.
55+
56+
Example:
57+
Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
58+
Output: 3
59+
60+
Constraints:
61+
- 1 <= students.length, sandwiches.length <= 100
62+
- students.length == sandwiches.length
63+
- sandwiches[i] is 0 or 1.
64+
- students[i] is 0 or 1.
65+
'''
66+
#Difficulty: Easy
67+
#55 / 55 test cases passed.
68+
#Runtime: 36 ms
69+
#Memory Usage: 14.4 MB
70+
71+
#Runtime: 36 ms, faster than 84.36% of Python3 online submissions for Number of Students Unable to Eat Lunch.
72+
#Memory Usage: 14.4 MB, less than 23.56% of Python3 online submissions for Number of Students Unable to Eat Lunch.
73+
74+
class Solution:
75+
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
76+
stuck_in_queue = 0
77+
while stuck_in_queue < len(students):
78+
student = students.pop(0)
79+
if sandwiches[0] == student:
80+
sandwiches.pop(0)
81+
stuck_in_queue = 0
82+
else:
83+
students.append(student)
84+
stuck_in_queue += 1
85+
return stuck_in_queue

0 commit comments

Comments
 (0)