Skip to content

Commit 5b5131c

Browse files
committed
Create: 1046-last-stone-weight
1 parent 66ab0a3 commit 5b5131c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

rust/1046-last-stone-weight.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn last_stone_weight(stones: Vec<i32>) -> i32 {
3+
let mut stones_heap = std::collections::BinaryHeap::new();
4+
for stone in stones {
5+
stones_heap.push(stone);
6+
}
7+
8+
while stones_heap.len() > 1 {
9+
let first = stones_heap.pop().unwrap();
10+
let second = stones_heap.pop().unwrap();
11+
stones_heap.push(first - second);
12+
}
13+
14+
match stones_heap.peek() {
15+
Some(val) => *val,
16+
None => 0,
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)