[LeetCode] 1381. Design a Stack With Increment Operation
Design a stack which supports the following operations.
Implement theCustomStack
class:
CustomStack(int maxSize)
Initializes the object withmaxSize
which is the maximum number of elements in the stack or do nothing if the stack reached themaxSize
.void push(int x)
Addsx
to the top of the stack if the stack hasn't reached themaxSize
int pop()
Pops and returns the top of stack or-1if the stack is empty.void inc(int k, int val)
Increments the bottomk
elements of the stack byval
. If there are less thank
elements in the stack, just increment all the elements in the stack.
Example 1:
Input ["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"] [[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]] Output [null,null,null,2,null,null,null,null,null,103,202,201,-1] Explanation CustomStack customStack = new CustomStack(3); // Stack is Empty [] customStack.push(1); // stack becomes [1] customStack.push(2); // stack becomes [1, 2] customStack.pop(); // return 2 --> Return top of the stack 2, stack becomes [1] customStack.push(2); // stack becomes [1, 2] customStack.push(3); // stack becomes [1, 2, 3] customStack.push(4); // stack still [1, 2, 3], Don't add another elements as size is 4 customStack.increment(5, 100); // stack becomes [101, 102, 103] customStack.increment(2, 100); // stack becomes [201, 202, 103] customStack.pop(); // return 103 --> Return top of the stack 103, stack becomes [201, 202] customStack.pop(); // return 202 --> Return top of the stack 102, stack becomes [201] customStack.pop(); // return 201 --> Return top of the stack 101, stack becomes [] customStack.pop(); // return -1 --> Stack is empty return -1.
Constraints:
1 <= maxSize <= 1000
1 <= x <= 1000
1 <= k <= 1000
0 <= val <= 100
- At most
1000
calls will be made to each method ofincrement
,push
andpop
each separately.
設計一個支援增量操作的棧。
請你設計一個支援下述操作的棧。
實現自定義棧類 CustomStack :
CustomStack(int maxSize):用 maxSize 初始化物件,maxSize 是棧中最多能容納的元素數量,棧在增長到 maxSize 之後則不支援 push 操作。
void push(int x):如果棧還未增長到 maxSize ,就將 x 新增到棧頂。
int pop():彈出棧頂元素,並返回棧頂的值,或棧為空時返回 -1 。
void inc(int k, int val):棧底的 k 個元素的值都增加 val 。如果棧中元素總數小於 k ,則棧中的所有元素都增加 val 。來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/design-a-stack-with-increment-operation
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。
這是一道設計題,除了支援正常的關於棧的操作,還多一個功能,支援增量操作。這道題我給出兩種做法,一種是用list模擬stack,另一種是利用一個額外陣列去記錄增量。
首先是用list模擬stack,空間O(n)。
push O(1) - 只要stack的size小於maxSize,就可以往stack增加元素
pop O(1) - 只要stack裡面還有東西,就可以往外pop,否則就返回-1
increment O(n) - 用一個for迴圈去強行修改list裡面對應下標範圍內的元素
1 class CustomStack { 2 private List<Integer> stack = new ArrayList<>(); 3 private int size; 4 5 public CustomStack(int maxSize) { 6 size = maxSize; 7 } 8 9 public void push(int x) { 10 if (stack.size() < size) { 11 stack.add(x); 12 } 13 } 14 15 public int pop() { 16 return stack.isEmpty() ? -1 : stack.remove(stack.size() - 1); 17 } 18 19 public void increment(int k, int val) { 20 for (int i = 0; i < k && i < stack.size(); i++) { 21 stack.set(i, stack.get(i) + val); 22 } 23 } 24 } 25 26 /** 27 * Your CustomStack object will be instantiated and called as such: 28 * CustomStack obj = new CustomStack(maxSize); 29 * obj.push(x); 30 * int param_2 = obj.pop(); 31 * obj.increment(k,val); 32 */
再來是利用一個額外陣列去記錄增量。這裡我著重解釋一下increment函式,首先我們需要一個和maxSize等長的陣列來記錄stack裡面每個元素可能的增量,介於K有可能大於stack目前的size,所以在做increment的時候需要在K和stack.size()之間取較小值。當需要改動的數字個數 i > 0的時候,我們需要把增量val加到陣列對應的下標 i - 1 上。這樣當我們做pop操作的時候,可以順帶取到這個增量add[i]。同時因為增量影響的是下標從0到 i - 1範圍內的所有數字,所以當i > 0的時候,add[i] 上的增量val也需要累加到 add[i - 1] 上。
空間O(n)
push O(1) - 只要stack的size小於maxSize,就可以往stack增加元素
pop O(1) - 只要stack裡面還有東西,就可以往外pop,否則就返回-1
increment O(1) - 用一個for迴圈去強行修改list裡面對應下標範圍內的元素
1 class CustomStack { 2 private int n; 3 int[] add; 4 Stack<Integer> stack; 5 6 public CustomStack(int maxSize) { 7 n = maxSize; 8 add = new int[n]; 9 stack = new Stack<>(); 10 } 11 12 public void push(int x) { 13 if (stack.size() < n) { 14 stack.push(x); 15 } 16 } 17 18 public int pop() { 19 int i = stack.size() - 1; 20 if (i < 0) { 21 return -1; 22 } 23 if (i > 0) { 24 add[i - 1] += add[i]; 25 } 26 int res = stack.pop() + add[i]; 27 add[i] = 0; 28 return res; 29 } 30 31 public void increment(int k, int val) { 32 int i = Math.min(k, stack.size()); 33 if (i > 0) { 34 add[i - 1] += val; 35 } 36 } 37 } 38 39 /** 40 * Your CustomStack object will be instantiated and called as such: 41 * CustomStack obj = new CustomStack(maxSize); 42 * obj.push(x); 43 * int param_2 = obj.pop(); 44 * obj.increment(k,val); 45 */