File tree Expand file tree Collapse file tree 1 file changed +10
-2
lines changed Expand file tree Collapse file tree 1 file changed +10
-2
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
func topKFrequent( _ nums: [ Int ] , _ k: Int ) -> [ Int ] {
3
3
var dictionary : [ Int : Int ] = [ : ]
4
- for num in nums {
4
+ for num in nums { // for loop๋ฅผ ๋๋ฉด์ O(n)์ ์๊ฐ๋ณต์ก๋
5
5
dictionary [ num, default: 0 ] += 1
6
+ // Swift์์ dictionary ๊ฒ์์ ์๊ฐ๋ณต์ก๋๋ O(1)
6
7
}
7
8
8
- return dictionary. sorted ( by: { $0. value > $1. value } ) . prefix ( k) . map ( \. key)
9
+ return dictionary
10
+ . sorted ( by: { $0. value > $1. value } )
11
+ // dictionary sorted()์ ์๊ฐ๋ณต์ก๋ O(n log n)
12
+ . prefix ( k)
13
+ // k์ ๊ฐ์๋งํผ ํ์ํฉ๋๋ค. ๊ณ ๋ก ์๊ฐ๋ณต์ก๋๋ O(n)
14
+ . map ( \. key)
15
+ // prefix์์ k๋งํผ ํ์ํ์์ต๋๋ค.
16
+ // .map์ ์๊ฐ๋ณต์ก๋๋ O(n)์
๋๋ค.
9
17
}
10
18
}
11
19
You canโt perform that action at this time.
0 commit comments