File tree Expand file tree Collapse file tree 2 files changed +33
-4
lines changed Expand file tree Collapse file tree 2 files changed +33
-4
lines changed Original file line number Diff line number Diff line change 1
-
2
1
// NOTE: 백준 N과 M문제와 거의 동일한 문제
3
2
// memo(n) = memo(n-1)의 경우에 수에서 1칸을 올라가는 경우 + memo(n-2) 의 경우의 수에서 2 칸을 올라가는 경우의 점화식으로 표현됨.
4
-
5
-
6
3
// 시간 복잡도: O(n)
7
4
class Solution {
8
5
public int climbStairs (int n ) {
@@ -21,4 +18,4 @@ public int climbStairs(int n) {
21
18
22
19
return memo [n ];
23
20
}
24
- }
21
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .ArrayList ;
2
+ import java .util .HashMap ;
3
+ import java .util .List ;
4
+ import java .util .Map ;
5
+
6
+
7
+ // O(nlogn) 시간복잡도.
8
+
9
+ class Solution {
10
+ public int [] topKFrequent (int [] nums , int k ) {
11
+ Map <Integer , Integer > freqMap = new HashMap <>();
12
+ int [] res = new int [k ];
13
+
14
+ for (int i = 0 ; i < nums .length ; i ++) {
15
+ if (freqMap .containsKey (nums [i ])) {
16
+ freqMap .put (nums [i ], freqMap .get (nums [i ]) + 1 );
17
+ } else {
18
+ freqMap .put (nums [i ], 1 );
19
+ }
20
+ }
21
+
22
+ List <Map .Entry <Integer , Integer >> entList = new ArrayList <>(freqMap .entrySet ());
23
+
24
+ entList .sort ((a , b ) -> b .getValue ().compareTo (a .getValue ()));
25
+
26
+ for (int i = 0 ; i < res .length ; i ++) {
27
+ res [i ] = entList .get (i ).getKey ();
28
+ }
29
+
30
+ return res ;
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments