-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincrementStack.js
68 lines (49 loc) · 1.34 KB
/
incrementStack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Increment stack
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
Input:
Output:
Input:
Output:
Constraints:
Edge Case:
Time Complexity:
push() / pop() operates at linear O(n) because worst case is we have
increment() O(n)
Space Complexity: O(1) - no increase to auxiliary space
Optimized version:
Time Complexity:
Space Complexity:
*/
class CustomStack {
constructor(maxSize) {
this.capacity = maxSize;
this.stack = [];
}
push(val) {
if (this.stack.length < this.capacity) {
this.stack.push(val);
}
}
pop() {
if (this.stack.length) return this.stack.pop();
else return -1;
}
increment(k, val) {
if (k > this.stack.length) {
k = this.stack.length - 1;
} else k--;
while (k > -1) {
this.stack[k] += val;
k--;
}
}
}