forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0347-top-k-frequent-elements.rs
48 lines (38 loc) · 1.24 KB
/
0347-top-k-frequent-elements.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::collections::HashMap;
use std::cmp::Ordering;
impl Solution {
pub fn top_k_frequent(nums: Vec<i32>, k: i32) -> Vec<i32> {
let mut map: HashMap<i32, i32> = HashMap::new();
for n in nums{
*map.entry(n).and_modify(|val| *val+=1).or_default();
}
let mut freq: Vec<(i32, i32)> = map.into_iter().collect();
let res = match (k as usize).cmp(&freq.len()){
Ordering::Equal => &freq,
_ => quick_select(&mut freq, k),
};
res.into_iter()
.map(|&n| n.0)
.collect()
}
}
pub fn quick_select(slice: &mut [(i32, i32)], k: i32) -> &[(i32, i32)]{
let (mut pivot, mut i, mut j) = (0, 1, 1);
for index in 1..slice.len(){
if slice[index].1 >= slice[pivot].1{
slice.swap(index, j);
}else{
slice.swap(index, i);
i+=1;
}
j+=1;
}
slice.swap(pivot, i - 1);
pivot = i - 1;
let larger_items = (j - pivot) as i32;
match larger_items.cmp(&k) {
Ordering::Less => quick_select(&mut slice[0..j], k),
Ordering::Greater => quick_select(&mut slice[pivot + 1..j], k),
Ordering::Equal => &slice[pivot..j],
}
}