From c7048b8b191ddff943d771cd05622f9f8af47102 Mon Sep 17 00:00:00 2001 From: ktkovachev <143198904+ktkovachev@users.noreply.github.com> Date: Mon, 8 Apr 2024 16:04:43 +0100 Subject: [PATCH] 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). --- ...00_number_of_students_unable_to_eat_lunch.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 python/1700_number_of_students_unable_to_eat_lunch.py diff --git a/python/1700_number_of_students_unable_to_eat_lunch.py b/python/1700_number_of_students_unable_to_eat_lunch.py new file mode 100644 index 0000000..5bcafd8 --- /dev/null +++ b/python/1700_number_of_students_unable_to_eat_lunch.py @@ -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)