My LeetCode grinding. Trying to do a problem a day.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.rs 571B

1234567891011121314151617181920212223
  1. pub fn last_stone_weight(mut stones: Vec<i32>) -> i32 {
  2. if stones.len() == 1 {
  3. return *stones.get(0).unwrap();
  4. } else if stones.len() == 0 {
  5. return 0;
  6. }
  7. // sort
  8. stones.sort_by(|a, b| b.cmp(a));
  9. // remove the max twice
  10. let m1 = stones.remove(0);
  11. let m2 = stones.remove(0);
  12. // add result, if any
  13. if m1 != m2 {
  14. stones.push(m1 - m2);
  15. }
  16. // go again
  17. return last_stone_weight(stones);
  18. }
  19. pub fn main() {
  20. println!("Expected: 1");
  21. println!("Got: {}", last_stone_weight(vec![2, 7, 4, 1, 8, 1]));
  22. }