-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01046. Last Stone Weight.rs
32 lines (29 loc) · 1.03 KB
/
01046. Last Stone Weight.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
impl Solution {
pub fn last_stone_weight(stones: Vec<i32>) -> i32 {
// Convert to mutable vector
let mut mut_stones = stones.to_vec();
let mut size = mut_stones.len();
loop {
// If only one element is left, return it
if (size == 1) {
return mut_stones[0];
}
// If no elements are left, return 0
if (size == 0) {
return 0;
}
// Sort the vector
mut_stones.sort();
// If second last and last vector are same, remove both, else assign second last to difference of the two and remove last
if (mut_stones[size - 1] == mut_stones[size - 2]) {
mut_stones.pop();
mut_stones.pop();
size -= 2;
} else {
mut_stones[size - 2] = mut_stones[size - 1] - mut_stones[size - 2];
mut_stones.pop();
size -= 1;
}
}
}
}