diff --git a/Algorithms/347. Top K Frequent Elements/347. Top K Frequent Elements.go b/Algorithms/347. Top K Frequent Elements/347. Top K Frequent Elements.go new file mode 100644 index 00000000..04d47fd4 --- /dev/null +++ b/Algorithms/347. Top K Frequent Elements/347. Top K Frequent Elements.go @@ -0,0 +1,53 @@ +package leetcode + +import "container/heap" + +func topKFrequent(nums []int, k int) []int { + m := make(map[int]int) + for _, n := range nums { + m[n]++ + } + q := PriorityQueue{} + for key, count := range m { + heap.Push(&q, &Item{key: key, count: count}) + } + var result []int + for len(result) < k { + item := heap.Pop(&q).(*Item) + result = append(result, item.key) + } + return result +} + +type Item struct { + key int + count int +} + +// A PriorityQueue implements heap.Interface and holds Items. +type PriorityQueue []*Item + +func (pq PriorityQueue) Len() int { + return len(pq) +} + +func (pq PriorityQueue) Less(i, j int) bool { + // 注意:因为golang中的heap是按最小堆组织的,所以count越大,Less()越小,越靠近堆顶. + return pq[i].count > pq[j].count +} + +func (pq PriorityQueue) Swap(i, j int) { + pq[i], pq[j] = pq[j], pq[i] +} + +func (pq *PriorityQueue) Push(x interface{}) { + item := x.(*Item) + *pq = append(*pq, item) +} + +func (pq *PriorityQueue) Pop() interface{} { + n := len(*pq) + item := (*pq)[n-1] + *pq = (*pq)[:n-1] + return item +} diff --git a/Algorithms/347. Top K Frequent Elements/347. Top K Frequent Elements_test.go b/Algorithms/347. Top K Frequent Elements/347. Top K Frequent Elements_test.go new file mode 100644 index 00000000..8d28f9ad --- /dev/null +++ b/Algorithms/347. Top K Frequent Elements/347. Top K Frequent Elements_test.go @@ -0,0 +1,48 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question347 struct { + para347 + ans347 +} + +// para 是参数 +// one 代表第一个参数 +type para347 struct { + one []int + two int +} + +// ans 是答案 +// one 代表第一个答案 +type ans347 struct { + one []int +} + +func Test_Problem347(t *testing.T) { + + qs := []question347{ + + question347{ + para347{[]int{1, 1, 1, 2, 2, 3}, 2}, + ans347{[]int{1, 2}}, + }, + + question347{ + para347{[]int{1}, 1}, + ans347{[]int{1}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 347------------------------\n") + + for _, q := range qs { + _, p := q.ans347, q.para347 + fmt.Printf("【input】:%v 【output】:%v\n", p, topKFrequent(p.one, p.two)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/347. Top K Frequent Elements/README.md b/Algorithms/347. Top K Frequent Elements/README.md new file mode 100644 index 00000000..5db21c2d --- /dev/null +++ b/Algorithms/347. Top K Frequent Elements/README.md @@ -0,0 +1,33 @@ +# [347. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) + +## 题目 + +Given a non-empty array of integers, return the k most frequent elements. + +Example 1: + +```c +Input: nums = [1,1,1,2,2,3], k = 2 +Output: [1,2] +``` + +Example 2: + +```c +Input: nums = [1], k = 1 +Output: [1] +``` + +Note: + +- You may assume k is always valid, 1 ≤ k ≤ number of unique elements. +- Your algorithm's time complexity must be better than O(n log n), where n is the array's size. + + +## 题目大意 + +给一个非空的数组,输出前 K 个频率最高的元素。 + +这一题是考察优先队列的题目。把数组构造成一个优先队列,输出前 K 个即可。 + +