-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.rs
30 lines (21 loc) · 977 Bytes
/
01.rs
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
// https://leetcode.com/submissions/detail/
use std::collections::HashMap;
impl Solution {
pub fn can_reorder_doubled(arr: Vec<i32>) -> bool {
let mut count: HashMap<i32, i32> = HashMap::new();
arr.iter().for_each(|num| { count.entry(*num).and_modify(|c| *c += 1).or_insert(1); });
// println!("{:?}", count);
let mut sorted: Vec<i32> = arr.clone();
sorted.sort_by(|lhv, rhv| lhv.abs().cmp(&rhv.abs()));
// println!("{:?}", sorted);
for num in sorted.into_iter() {
// println!("{}, {:?}", num, count.get(&num));
if *count.get(&num).unwrap_or(&0) == 0 { continue }
if *count.get(&(num * 2)).unwrap_or(&0) <= 0 { return false }
count.entry(num).and_modify(|c| *c -= 1);
count.entry(num * 2).and_modify(|c| *c -= 1);
// println!("{:?}", count);
}
true
}
}