347. Top K Frequent Elements Medium

@problem@discussion
#Array#Hash Table#Divide and Conquer#Sorting#Heap (Priority Queue)#Bucket Sort#Counting#Quickselect



1/**
2 * [347] Top K Frequent Elements
3 *
4 * Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
5 *  
6 * Example 1:
7 * Input: nums = [1,1,1,2,2,3], k = 2
8 * Output: [1,2]
9 * Example 2:
10 * Input: nums = [1], k = 1
11 * Output: [1]
12 *  
13 * Constraints:
14 * 
15 * 	1 <= nums.length <= 10^5
16 * 	-10^4 <= nums[i] <= 10^4
17 * 	k is in the range [1, the number of unique elements in the array].
18 * 	It is guaranteed that the answer is unique.
19 * 
20 *  
21 * Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
22 * 
23 */
24pub struct Solution {}
25
26// problem: https://leetcode.com/problems/top-k-frequent-elements/
27// discuss: https://leetcode.com/problems/top-k-frequent-elements/discuss/?currentPage=1&orderBy=most_votes&query=
28
29// submission codes start here
30
31impl Solution {
32    pub fn top_k_frequent(nums: Vec<i32>, k: i32) -> Vec<i32> {
33        vec![]
34    }
35}
36
37// submission codes end
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn test_347() {
45    }
46}
47


Back
© 2025 bowen.ge All Rights Reserved.