-
Notifications
You must be signed in to change notification settings - Fork 4
/
design-a-stack-with-increment-operation.rs
executable file
·146 lines (140 loc) · 3.8 KB
/
design-a-stack-with-increment-operation.rs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 1381. Design a Stack With Increment Operation
// 🟠 Medium
//
// https://leetcode.com/problems/design-a-stack-with-increment-operation/
//
// Tags: Array - Stack - Design
/// Simple stack, add a check of max size before pushing and the increment method.
///
/// Space complexity: O(k) - We can hold a max of max_size elements.
///
/// Runtime 6 ms Beats 25%
/// Memory 3.05 MB Beats 87%
struct CustomStack {
stack: Vec<i32>,
size: usize,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl CustomStack {
/// Time complexity: O(1)
fn new(max_size: i32) -> Self {
Self {
stack: Vec::with_capacity(max_size as usize),
size: max_size as usize,
}
}
/// Time complexity: O(1)
fn push(&mut self, x: i32) {
if self.stack.len() < self.size {
self.stack.push(x);
}
}
/// Time complexity: O(1)
fn pop(&mut self) -> i32 {
match self.stack.pop() {
Some(x) => x,
None => -1,
}
}
/// Time complexity: O(k)
fn increment(&mut self, k: i32, val: i32) {
for i in 0..(k as usize).min(self.stack.len()) {
self.stack[i] += val;
}
}
}
// Tests.
fn main() {
let mut stack = CustomStack::new(3);
println!("\n\x1b[92m» Running tests...\x1b[0m");
let (mut success, mut failed) = (0, 0);
let (mut res, mut expected);
stack.push(1);
stack.push(2);
expected = 2;
res = stack.pop();
if res == expected {
success += 1;
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", success + failed);
} else {
failed += 1;
println!(
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
success + failed,
expected,
res
);
}
stack.push(2);
stack.push(3);
stack.push(4);
stack.increment(5, 100);
stack.increment(2, 100);
expected = 103;
res = stack.pop();
if res == expected {
success += 1;
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", success + failed);
} else {
failed += 1;
println!(
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
success + failed,
expected,
res
);
}
expected = 202;
res = stack.pop();
if res == expected {
success += 1;
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", success + failed);
} else {
failed += 1;
println!(
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
success + failed,
expected,
res
);
}
expected = 201;
res = stack.pop();
if res == expected {
success += 1;
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", success + failed);
} else {
failed += 1;
println!(
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
success + failed,
expected,
res
);
}
expected = -1;
res = stack.pop();
if res == expected {
success += 1;
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", success + failed);
} else {
failed += 1;
println!(
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
success + failed,
expected,
res
);
}
println!();
if failed == 0 {
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
} else if success == 0 {
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
} else {
println!("\x1b[31mx\x1b[95m {} tests failed!\x1b[0m", failed)
}
}