1234567891011121314151617181920212223 |
- pub fn last_stone_weight(mut stones: Vec<i32>) -> i32 {
- if stones.len() == 1 {
- return *stones.get(0).unwrap();
- } else if stones.len() == 0 {
- return 0;
- }
- // sort
- stones.sort_by(|a, b| b.cmp(a));
- // remove the max twice
- let m1 = stones.remove(0);
- let m2 = stones.remove(0);
- // add result, if any
- if m1 != m2 {
- stones.push(m1 - m2);
- }
- // go again
- return last_stone_weight(stones);
- }
-
- pub fn main() {
- println!("Expected: 1");
- println!("Got: {}", last_stone_weight(vec![2, 7, 4, 1, 8, 1]));
- }
|