1172. Dinner Plate Stacks Hard
1/**
2 * [1172] Dinner Plate Stacks
3 *
4 * You have an infinite number of stacks arranged in a row and numbered (left to right) from 0, each of the stacks has the same maximum capacity.
5 * Implement the DinnerPlates class:
6 *
7 * DinnerPlates(int capacity) Initializes the object with the maximum capacity of the stacks capacity.
8 * void push(int val) Pushes the given integer val into the leftmost stack with a size less than capacity.
9 * int pop() Returns the value at the top of the rightmost non-empty stack and removes it from that stack, and returns -1 if all the stacks are empty.
10 * int popAtStack(int index) Returns the value at the top of the stack with the given index index and removes it from that stack or returns -1 if the stack with that given index is empty.
11 *
12 *
13 * Example 1:
14 *
15 * Input
16 * ["DinnerPlates", "push", "push", "push", "push", "push", "popAtStack", "push", "push", "popAtStack", "popAtStack", "pop", "pop", "pop", "pop", "pop"]
17 * [[2], [1], [2], [3], [4], [5], [0], [20], [21], [0], [2], [], [], [], [], []]
18 * Output
19 * [null, null, null, null, null, null, 2, null, null, 20, 21, 5, 4, 3, 1, -1]
20 * Explanation:
21 * DinnerPlates D = DinnerPlates(2); // Initialize with capacity = 2
22 * D.push(1);
23 * D.push(2);
24 * D.push(3);
25 * D.push(4);
26 * D.push(5); // The stacks are now: 2 4
27 * 1 3 5
28 * ﹈ ﹈ ﹈
29 * D.popAtStack(0); // Returns 2. The stacks are now: 4
30 * 1 3 5
31 * ﹈ ﹈ ﹈
32 * D.push(20); // The stacks are now: 20 4
33 * 1 3 5
34 * ﹈ ﹈ ﹈
35 * D.push(21); // The stacks are now: 20 4 21
36 * 1 3 5
37 * ﹈ ﹈ ﹈
38 * D.popAtStack(0); // Returns 20. The stacks are now: 4 21
39 * 1 3 5
40 * ﹈ ﹈ ﹈
41 * D.popAtStack(2); // Returns 21. The stacks are now: 4
42 * 1 3 5
43 * ﹈ ﹈ ﹈
44 * D.pop() // Returns 5. The stacks are now: 4
45 * 1 3
46 * ﹈ ﹈
47 * D.pop() // Returns 4. The stacks are now: 1 3
48 * ﹈ ﹈
49 * D.pop() // Returns 3. The stacks are now: 1
50 * ﹈
51 * D.pop() // Returns 1. There are no stacks.
52 * D.pop() // Returns -1. There are still no stacks.
53 *
54 *
55 * Constraints:
56 *
57 * 1 <= capacity <= 2 * 10^4
58 * 1 <= val <= 2 * 10^4
59 * 0 <= index <= 10^5
60 * At most 2 * 10^5 calls will be made to push, pop, and popAtStack.
61 *
62 */
63pub struct Solution {}
64
65// problem: https://leetcode.com/problems/dinner-plate-stacks/
66// discuss: https://leetcode.com/problems/dinner-plate-stacks/discuss/?currentPage=1&orderBy=most_votes&query=
67
68// submission codes start here
69
70struct DinnerPlates {
71 false
72 }
73
74
75/**
76 * `&self` means the method takes an immutable reference.
77 * If you need a mutable reference, change it to `&mut self` instead.
78 */
79impl DinnerPlates {
80
81 fn new(capacity: i32) -> Self {
82
83 }
84
85 fn push(&self, val: i32) {
86
87 }
88
89 fn pop(&self) -> i32 {
90
91 }
92
93 fn pop_at_stack(&self, index: i32) -> i32 {
94
95 }
96}
97
98/**
99 * Your DinnerPlates object will be instantiated and called as such:
100 * let obj = DinnerPlates::new(capacity);
101 * obj.push(val);
102 * let ret_2: i32 = obj.pop();
103 * let ret_3: i32 = obj.pop_at_stack(index);
104 */
105
106// submission codes end
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111
112 #[test]
113 fn test_1172() {
114 }
115}
116
Back
© 2025 bowen.ge All Rights Reserved.