Member-only story

Can You Feed These Hungry Students On LeetCode?

A Peek Into Arrays, Stacks, Queues & Deques

Dr. Ashish Bamania
3 min readMay 4, 2024
Generated with DALL-E 3

I recently came across an interesting problem on Leetcode.

It goes like this —

There’s a school cafeteria that offers two kinds of sandwiches (denoted with 0 (circular shaped) and 1 (square shaped).

These sandwiches are put in a Stack.

There’s a queue of hungry students who have a particular preference for either of these sandwiches.

The student at the front of the queue will pick up the sandwich from the sandwich stack, and only eat it if its shape matches his/ her preference (weird, but okay!).

If not, they will simply leave the sandwich back in the stack and join again at the queue’s end.

This process continues till none of the students in the queue wants a sandwich and thus they are unable to eat.

Given two integer arrays students and sandwiches, our challenge is to find out how many students are unable to eat.

Note that in the sandwiches array, i = 0 is the top of the stack while in the students array, j = 0 is the front of the queue.

My First Attempt

--

--

Dr. Ashish Bamania
Dr. Ashish Bamania

Written by Dr. Ashish Bamania

🍰 I simplify the latest advances in AI, Quantum Computing & Software Engineering for you | 📰 Subscribe to my newsletter here: https://intoai.pub

Responses (3)