-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.rs
More file actions
35 lines (31 loc) · 727 Bytes
/
main.rs
File metadata and controls
35 lines (31 loc) · 727 Bytes
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
fn main() {
println!("Hello, world!");
}
struct Solution {}
impl Solution {
pub fn last_stone_weight(stones: Vec<i32>) -> i32 {
use std::collections::BinaryHeap;
let mut stones = BinaryHeap::from(stones);
while stones.len() >= 2 {
let x = stones.pop().unwrap();
let y = stones.pop().unwrap();
// x is guaranteed to >= y
if x > y {
stones.push(x - y);
}
}
if let Some(x) = stones.pop() {
x
} else {
0
}
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::last_stone_weight(vec![2,7,4,1,8,1]), 1);
}
}