Algorithm/Queue and Stack
-
Number of Students Unable to Eat LunchAlgorithm/Queue and Stack 2021. 11. 22. 23:59
The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers 0 and 1 respectively. All students stand in a queue. Each student either prefers square or circular sandwiches. The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step: If the student at the front of the queue prefers the sandw..
-
Number of Recent CallsAlgorithm/Queue and Stack 2021. 11. 22. 23:01
You have a RecentCounter class which counts the number of recent requests within a certain time frame. Implement the RecentCounter class: RecentCounter() Initializes the counter with zero recent requests. int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new..
-
Implement Queue using StacksAlgorithm/Queue and Stack 2021. 11. 22. 22:08
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: void push(int x) Pushes element x to the back of the queue. int pop() Removes the element from the front of the queue and returns it. int peek() Returns the element at the front of the queue. boolean..
-
Implement Stack using QueuesAlgorithm/Queue and Stack 2021. 11. 22. 20:55
https://leetcode.com/problems/implement-stack-using-queues/ Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the MyStack class: void push(int x) Pushes element x to the top of the stack. int pop() Removes the element on the top of the stack and returns it. int top() Re..