Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8082f1c

Browse files
committed
Merge branch 'master' of github.com:zwfang/leetcode
2 parents aeab9b4 + ebad4c1 commit 8082f1c

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ continually updating 😃.
3131
* [283. Move Zeroes(solution1)](./src/0283_move_zeroes/move_zeroes.go)   *`sliding window`*
3232
* [283. Move Zeroes(solution2)](./src/0283_move_zeroes/move_zeroes2.go)   *`sliding window`*
3333
* [303. Range Sum Query - Immutable](src/0303_range_sum_query/rsqim.go)
34+
* [347. Top K Frequent Elements](src/0347_top_k_frequent_elements/topkfe.go)   *`hash table;`*  *`heap`*
3435
* [349. Intersection of Two Arrays](./src/0349_intersection_of_2_arrays/intersection_of_two_arrays.go)   *`set`*
3536
* [350. Intersection of Two Arrays II](./src/0350_intersection_of_two_arrays2/intersection_of_two_arrays2.go)   *`hash table`*
3637
* [447. Number of Boomerangs](./src/0447_number_of_boomerangs/number_of_boomerangs.go)   *`hash table`*
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
347. Top K Frequent Elements
3+
https://leetcode.com/problems/top-k-frequent-elements/
4+
5+
Given a non-empty array of integers, return the k most frequent elements.
6+
7+
Note:
8+
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
9+
Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
10+
*/
11+
// time: 2019年01月08日
12+
13+
package topkfe
14+
15+
import "container/heap"
16+
17+
// using priority
18+
// time complexity: O(n log n)
19+
// space complexity: O(n)
20+
func topKFrequent(nums []int, k int) []int {
21+
count := make(map[int]int)
22+
for _, num := range nums {
23+
if _, ok := count[num]; !ok {
24+
count[num] = 1
25+
} else {
26+
count[num]++
27+
}
28+
}
29+
30+
nums_ := make(Nums, 0)
31+
32+
for num, cnt := range count {
33+
nums_ = append(nums_, Num{Val: num, Count: cnt})
34+
}
35+
heap.Init(&nums_)
36+
var res []int
37+
for i := 0; i < k; i++ {
38+
num := heap.Pop(&nums_).(Num)
39+
res = append(res, num.Val)
40+
}
41+
return res
42+
}
43+
44+
// Num stores its value and frequency as Count.
45+
type Num struct {
46+
Val int
47+
Count int
48+
}
49+
50+
// Nums struct for impl Interface
51+
type Nums []Num
52+
53+
// Len sort Interface
54+
func (n Nums) Len() int { return len(n) }
55+
56+
// Swap sort Interface
57+
func (n Nums) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
58+
59+
// Less sort Interface
60+
func (n Nums) Less(i, j int) bool { return n[i].Count >= n[j].Count }
61+
62+
// Push heap Interface
63+
func (n *Nums) Push(num interface{}) {
64+
m := num.(Num)
65+
*n = append(*n, m)
66+
}
67+
68+
// Pop heap Interface
69+
func (n *Nums) Pop() interface{} {
70+
res := (*n)[len(*n)-1]
71+
*n = (*n)[:len(*n)-1]
72+
return res
73+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package topkfe
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestTopKFrequent(t *testing.T) {
9+
nums := []int{1, 1, 1, 2, 2, 3}
10+
k := 2
11+
expected := []int{1, 2}
12+
if res := topKFrequent(nums, k); !reflect.DeepEqual(res, expected) {
13+
t.Errorf("expected %v, got %v", expected, res)
14+
}
15+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
|0343|[Integer Break](./0343_integer_break/integer_break.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
8383
|0344|[344. Reverse String](0344_reverse_string/reverse_string.go)|Easy|*`double index`*|
8484
|0345|[345. Reverse Vowels of a String](0345_reverse_vowels_of_a_string/reverse_vowels.go)|Easy|*`double index`*|
85+
|0347|[347. Top K Frequent Elements](0347_top_k_frequent_elements/topkfe.go)|Medium|*`map;`* *`heap;`* *`array`*|
8586
|0349|[Intersection of Two Arrays](./0349_intersection_of_2_arrays/intersection_of_two_arrays.go)|Easy|*`set`*|
8687
|0350| [Intersection of Two Arrays II](./0350_intersection_of_two_arrays2/intersection_of_two_arrays2.go)|Easy|*`map`*|
8788
|0376|[Wiggle Subsequence](./0376_wiggle_subsequence/wiggle_subsequence.go)|Medium|*`dp`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /