Solve 1700. Number of Students Unable to Eat Lunch

Using a deque to improve the speed of removing from the left side of the queue and rotating students to the back of the queue (from O(n) to O(1) amortized).
This commit is contained in:
ktkovachev 2024-04-08 16:04:43 +01:00 committed by GitHub
parent 98b2830568
commit c7048b8b19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,17 @@
class Solution:
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
students = deque(students)
sandwiches = deque(sandwiches)
took_this_pass = True
while took_this_pass:
took_this_pass = False
for i in range(len(students)):
student = students[0]
sandwich = sandwiches[0]
if student == sandwich:
students.popleft()
sandwiches.popleft()
took_this_pass = True
else:
students.rotate(-1)
return len(students)