-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from stada526/f/1046
1046. Last Stone Weight
- Loading branch information
Showing
3 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::common::heap::MaxHeap; | ||
|
||
struct Solution {} | ||
|
||
impl Solution { | ||
pub fn last_stone_weight(stones: Vec<i32>) -> i32 { | ||
let mut max_heap = MaxHeap::new(stones); | ||
while max_heap.len() > 1 { | ||
let y = max_heap.pop().unwrap(); | ||
let x = max_heap.pop().unwrap(); | ||
let diff = y - x; | ||
if diff != 0 { | ||
max_heap.push(diff) | ||
} | ||
} | ||
|
||
return if max_heap.len() == 0 { | ||
0 | ||
} else { | ||
max_heap.pop().unwrap() | ||
}; | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test() { | ||
let stones = vec![2, 7, 4, 1, 8, 1]; | ||
let res = Solution::last_stone_weight(stones); | ||
assert_eq!(res, 1) | ||
} | ||
} |