1381. Design a Stack With Increment Operation Medium

@problem@discussion
#Array#Stack#Design



1/**
2 * [1381] Design a Stack With Increment Operation
3 *
4 * Design a stack which supports the following operations.
5 * Implement the CustomStack class:
6 * 
7 * 	CustomStack(int maxSize) Initializes the object with maxSize which is the maximum number of elements in the stack or do nothing if the stack reached the maxSize.
8 * 	void push(int x) Adds x to the top of the stack if the stack hasn't reached the maxSize.
9 * 	int pop() Pops and returns the top of stack or -1 if the stack is empty.
10 * 	void inc(int k, int val) Increments the bottom k elements of the stack by val. If there are less than k elements in the stack, just increment all the elements in the stack.
11 * 
12 *  
13 * Example 1:
14 * 
15 * Input
16 * ["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
17 * [[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
18 * Output
19 * [null,null,null,2,null,null,null,null,null,103,202,201,-1]
20 * Explanation
21 * CustomStack customStack = new CustomStack(3); // Stack is Empty []
22 * customStack.push(1);                          // stack becomes [1]
23 * customStack.push(2);                          // stack becomes [1, 2]
24 * customStack.pop();                            // return 2 --> Return top of the stack 2, stack becomes [1]
25 * customStack.push(2);                          // stack becomes [1, 2]
26 * customStack.push(3);                          // stack becomes [1, 2, 3]
27 * customStack.push(4);                          // stack still [1, 2, 3], Don't add another elements as size is 4
28 * customStack.increment(5, 100);                // stack becomes [101, 102, 103]
29 * customStack.increment(2, 100);                // stack becomes [201, 202, 103]
30 * customStack.pop();                            // return 103 --> Return top of the stack 103, stack becomes [201, 202]
31 * customStack.pop();                            // return 202 --> Return top of the stack 102, stack becomes [201]
32 * customStack.pop();                            // return 201 --> Return top of the stack 101, stack becomes []
33 * customStack.pop();                            // return -1 --> Stack is empty return -1.
34 * 
35 *  
36 * Constraints:
37 * 
38 * 	1 <= maxSize <= 1000
39 * 	1 <= x <= 1000
40 * 	1 <= k <= 1000
41 * 	0 <= val <= 100
42 * 	At most 1000 calls will be made to each method of increment, push and pop each separately.
43 * 
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/design-a-stack-with-increment-operation/
48// discuss: https://leetcode.com/problems/design-a-stack-with-increment-operation/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52struct CustomStack {
53        false
54    }
55
56
57/** 
58 * `&self` means the method takes an immutable reference.
59 * If you need a mutable reference, change it to `&mut self` instead.
60 */
61impl CustomStack {
62
63    fn new(maxSize: i32) -> Self {
64        
65    }
66    
67    fn push(&self, x: i32) {
68        
69    }
70    
71    fn pop(&self) -> i32 {
72        
73    }
74    
75    fn increment(&self, k: i32, val: i32) {
76        
77    }
78}
79
80/**
81 * Your CustomStack object will be instantiated and called as such:
82 * let obj = CustomStack::new(maxSize);
83 * obj.push(x);
84 * let ret_2: i32 = obj.pop();
85 * obj.increment(k, val);
86 */
87
88// submission codes end
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    #[test]
95    fn test_1381() {
96    }
97}
98


Back
© 2025 bowen.ge All Rights Reserved.