/
lazy.rs
74 lines (60 loc) · 1.67 KB
/
lazy.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
// ------------------------------------------------------ snip ------------------------------------------------------ //
pub struct CustomStack {
buffer: Box<[(i32, i32)]>,
length: usize,
}
impl CustomStack {
fn new(max_size: i32) -> Self {
Self {
buffer: vec![(0, 0); max_size as _].into_boxed_slice(),
length: 0,
}
}
fn push(&mut self, x: i32) {
if let Some(slot) = self.buffer.get_mut(self.length) {
*slot = (x, 0);
self.length += 1;
}
}
fn pop(&mut self) -> i32 {
if let Some(&top) = self.buffer.get(self.length.wrapping_sub(1)) {
self.length -= 1;
if let Some(new_top) = self.buffer.get_mut(self.length.wrapping_sub(1)) {
new_top.1 += top.1;
}
top.0 + top.1
} else {
-1
}
}
fn increment(&mut self, k: i32, val: i32) {
if let Some((_, extra)) = self
.buffer
.get_mut((k as u32 as usize).min(self.length).wrapping_sub(1))
{
*extra += val;
}
}
}
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl super::CustomStack for CustomStack {
fn new(max_size: i32) -> Self {
Self::new(max_size)
}
fn push(&mut self, x: i32) {
self.push(x);
}
fn pop(&mut self) -> i32 {
self.pop()
}
fn increment(&mut self, k: i32, val: i32) {
self.increment(k, val);
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::CustomStack>();
}
}