Exercises
We will resolve an exercise from LeetCode using the concepts we learned in this chapter.
Number of Students Unable to Eat Lunch
The exercise we will resolve is the 1700. Number of Students Unable to Eat Lunch problem available at https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/.
When resolving the problem using JavaScript or TypeScript, we will need to add our logic inside the function function countStudents(students: number[], sandwiches: number[]): number {}
, which receives a queue of students
that would prefer to eat a sandwich 0 or 1 and a stack of sandwiches
, which will have the same size.
This is a simulation exercise, according to the problem's description:
- If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue.
- Otherwise, they will leave it and go to the queue's end.
- This continues until none of the queue students want to take the top sandwich and are thus unable to...