-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.rs
48 lines (46 loc) · 1.54 KB
/
Solution.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
impl Solution {
#[allow(dead_code)]
pub fn asteroid_collision(asteroids: Vec<i32>) -> Vec<i32> {
let mut ret_stack = Vec::new();
for &a in &asteroids {
if ret_stack.is_empty() {
ret_stack.push(a);
continue;
}
if a > 0 {
ret_stack.push(a);
continue;
}
// Otherwise, peek the top element in the current stack
if a < 0 {
if *ret_stack.last().unwrap() < 0 {
ret_stack.push(a);
continue;
}
let mut explode_flag = false;
while !ret_stack.is_empty() && *ret_stack.last().unwrap() > 0 {
let cur_res = *ret_stack.last().unwrap() + a;
if cur_res < 0 {
// |a| > |top()|
assert_ne!(ret_stack.pop(), None);
} else if cur_res == 0 {
// |a| == |top()|
explode_flag = true;
assert_ne!(ret_stack.pop(), None);
break;
} else {
// |a| < |top()|
explode_flag = true;
break;
}
}
if !explode_flag {
ret_stack.push(a);
}
continue;
}
assert!(false); // This is impossible
}
ret_stack
}
}