Number of Students Unable to Eat Lunch

Easy · Stack & Strings · Array · Stack · Queue · Simulation

Problem

A cafeteria serves round sandwiches (0) and square sandwiches (1). Students wait in a queue: students[j] is the type the j-th student prefers, with index 0 at the front. The sandwiches sit in a stack: sandwiches[i] is the i-th from the top. There are as many sandwiches as students.

Repeat: if the student at the front prefers the top sandwich, they take it and leave; otherwise they walk to the back of the queue. This stops when no student still in the queue wants the top sandwich.

Return the number of students who are left without lunch.

Examples

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

Students rotate until each finds a match; both types are wanted twice and offered twice.

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

The only student who wants 0 takes the first sandwich; nobody wants the next 0, so three students go hungry.

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

The only student wants a square sandwich.

Constraints

Approaches

Follow-up

The rotation makes the order irrelevant, so two counters replace the queue. The use cases add a third sandwich type and ask how many students must change their preference so that everybody eats.

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…