-
Notifications
You must be signed in to change notification settings - Fork 0
/
top_k_frequent_elements.rs
68 lines (59 loc) · 1.54 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::collections::BinaryHeap;
use std::collections::HashMap;
#[derive(Copy, Clone, PartialOrd, PartialEq, Hash, Ord, Eq)]
struct State {
count: usize,
num: i32,
}
impl State {
fn new(num: i32) -> Self {
State { count: 1, num }
}
fn increment(&mut self) {
self.count += 1;
}
}
/// Given an integer array `nums` and an integer `k`, return the `k` most
/// frequent elements. You may return the answer in any order.
struct Solution;
impl Solution {
pub fn top_k_frequent(nums: Vec<i32>, k: i32) -> Vec<i32> {
let mut counts: HashMap<i32, State> = HashMap::new();
for num in nums {
counts
.entry(num)
.and_modify(|s| {
s.increment();
})
.or_insert(State::new(num));
}
let mut max_heap = BinaryHeap::new();
for count in counts {
max_heap.push(count.1);
}
let mut result = Vec::new();
for _ in 0..k as usize {
let num = max_heap.pop().unwrap().num;
result.push(num);
}
result
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let nums = vec![1, 1, 1, 2, 2, 3];
let k = 2;
let result = Solution::top_k_frequent(nums, k);
assert_eq!(result, vec![1, 2]);
}
#[test]
fn example_2() {
let nums = vec![1];
let k = 1;
let result = Solution::top_k_frequent(nums, k);
assert_eq!(result, vec![1]);
}
}