Problem 241721 · easy · Level 02 Linear Data Structures

Cafeteria Trays

queues · stacks · simulation

Students queue for lunch. Each student wants either a hot tray (1) or a cold tray (0); students lists their preferences in queue order. The trays are stacked; trays[0] is the tray on top of the stack.

The student at the front looks at the top tray. If it matches their preference they take it and leave; otherwise they go to the back of the queue. This continues until no student in the queue wants the tray on top. Return how many students are left without a tray.

Examples

Input:  students = [1, 1, 0, 0], trays = [0, 1, 0, 1]
Output: 0

Input:  students = [1, 1, 1, 0, 0, 1], trays = [1, 0, 0, 0, 1, 1]
Output: 3

Constraints

  • 1 <= len(students) == len(trays) <= 10**4
  • Values are 0 or 1
  • Target: O(n) time

Goals

  • Combine a queue of people with a stack of items
  • Detect when the simulation can never progress
Starting Python…