1700-number-of-students-unable-to-eat-lunch¶
Try it on leetcode
Description¶
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
Constraints:
1 <= students.length, sandwiches.length <= 100students.length == sandwiches.lengthsandwiches[i]is0or1.students[i]is0or1.
Solution(Python)¶
from collections import deque
class Solution:
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
return self.counting(students, sandwiches)
# cafeteria -> students fifo
# sandwich stack lifo
# simulate [1,1,0,0] [0,1,0,1]
# [1001] 0101
# 0011 0101
# 011 101
# 110 101
# 10 01
# 01 01
# 1 1
# [] []
# 111001 100011 ]
# 11001 00011
# 10011 00011
# 00111 00011
# 111 011
# How do we know when none of the students in the queue want to take the top sandwich?
# We can keep track of when we last served a student using the variable lastServed. If we are unable to serve a student, we increment lastServed. When we do serve a student, we reset lastServed to zero. When lastServed reaches the same size as the queue, we know we have offered the top sandwich to every student in the queue, so we stop the lunch process.
# After serving all the sandwiches we can, the remaining students in the queue are the unserved students.
# Time complexity: O(m*N)
# spac ecomplexity: P(m*n)
def simulate(self, students: List[int], sandwiches: List[int]) -> int:
studentQueue = deque(students)
sandwichStack = sandwiches[::-1]
last_served = 0
while len(studentQueue) >0 and last_served < len(students):
curr_student = studentQueue[0] # 1
curr_sandwich = sandwichStack[-1] # 0
if curr_student == curr_sandwich:
studentQueue.popleft()
sandwichStack.pop()
last_served = 0
else:
last_served += 1
studentQueue.append(studentQueue.popleft())
return len(studentQueue)
# observation
# 1: every student takes the top sandiwch 0
# 2: rest if students who dowsn't want what is in th topm pile
# If none of the students in the queue's preference matches the top sandwich, none of the remaining students can eat.
#
def counting(self, students: List[int], sandwiches: List[int]) -> int:
onecount = 0
zerocount = 0
for student in students:
if student:
onecount += 1
else:
zerocount += 1
for sandwich in sandwiches:
if sandwich == 1 and onecount == 0:
return zerocount
if sandwich == 0 and zerocount == 0:
return onecount
if sandwich == 0:
zerocount -= 1
else:
onecount -= 1
return 0