-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
61 lines (55 loc) · 1.38 KB
/
mod.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
struct State {
index: usize,
forward: bool,
max: u32,
}
impl State {
fn step(&mut self) {
if self.forward {
self.index += 1;
} else {
self.index -= 1;
}
}
fn next(heights: &[u32], lmax: usize, rmax: usize) -> State {
if heights[lmax] <= heights[rmax] {
State {
index: lmax + 1,
forward: true,
max: heights[lmax],
}
} else {
State {
index: rmax - 1,
forward: false,
max: heights[rmax],
}
}
}
}
pub fn capacity(heights: &[u32]) -> u32 {
let len = heights.len();
let mut capacity = 0;
let mut lmax = 0;
let mut rmax = len - 1;
let mut state = State::next(heights, lmax, rmax);
while (lmax < state.index) && (state.index < rmax) {
if state.max >= heights[state.index] {
capacity += state.max - heights[state.index];
state.step();
} else {
if state.forward {
lmax = state.index;
} else {
rmax = state.index;
}
state = State::next(heights, lmax, rmax);
}
}
capacity
}
#[test]
fn test_capacity() {
let heights = vec![2, 6, 3, 5, 2, 8, 1, 4, 2, 2, 5, 3, 5, 7, 4, 1];
assert_eq!(capacity(&heights), 35);
}