[LeetCode] 1381. Design a Stack With Increment Operation

Design a stack which supports the following operations.

Implement the CustomStack class:

  • 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.
  • void push(int x) Adds x to the top of the stack if the stack hasn't reached the maxSize.
  • int pop() Pops and returns the top of stack or -1 if the stack is empty.
  • 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.

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 of incrementpush and pop 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  */

 

LeetCode 题目总结

posted @ 2020-12-11 14:33  CNoodle  阅读(203)  评论(0编辑  收藏  举报