-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_1381_DesignStackWithIncrementOperation.cpp
87 lines (68 loc) · 1.61 KB
/
LC_1381_DesignStackWithIncrementOperation.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
https://leetcode.com/problems/design-a-stack-with-increment-operation/
1381. Design a Stack With Increment Operation
*/
class CustomStack {
public:
vector<int> st;
int size;
CustomStack(int maxSize) {
size = maxSize;
}
void push(int x) {
if(st.size() == size)
return; //ignore the element
st.push_back(x);
}
int pop() {
if(st.size() == 0)
return -1; // stack empty return -1
int value = st.back();
st.pop_back();
return value;
}
void increment(int k, int val) {
if(st.size() == 0)
return;
int sz = k; //
if(st.size() < k)
sz = st.size();
for(int i=0; i<sz; i++)
st[i] += val;
}
};
/*
class CustomStack {
vector<int> st;
int top;
public:
CustomStack(int maxSize) {
st.resize(maxSize);
top = -1;
}
void push(int x) {
if(st.size() == top+1)
return; //ignore the element
st[++top] = x;
}
int pop() {
if(top == -1)
return -1; // stack empty return -1
return st[top--];
}
void increment(int k, int val) {
if(top == -1)
return;
int sz = min(k, top+1); //
for(int i=0; i<sz; i++)
st[i] += val;
}
};
*/
/**
* Your CustomStack object will be instantiated and called as such:
* CustomStack* obj = new CustomStack(maxSize);
* obj->push(x);
* int param_2 = obj->pop();
* obj->increment(k,val);
*/