Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2216 from Jay-0331/rust/1046-last-ston…
Browse files Browse the repository at this point in the history
…e-weight

Create: 1046-last-stone-weight
  • Loading branch information
saip7795 authored Feb 6, 2023
2 parents 66ab0a3 + 5b5131c commit 1f23b2b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions rust/1046-last-stone-weight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
impl Solution {
pub fn last_stone_weight(stones: Vec<i32>) -> i32 {
let mut stones_heap = std::collections::BinaryHeap::new();
for stone in stones {
stones_heap.push(stone);
}

while stones_heap.len() > 1 {
let first = stones_heap.pop().unwrap();
let second = stones_heap.pop().unwrap();
stones_heap.push(first - second);
}

match stones_heap.peek() {
Some(val) => *val,
None => 0,
}
}
}

0 comments on commit 1f23b2b

Please sign in to comment.