File tree Expand file tree Collapse file tree 2 files changed +40
-6
lines changed Expand file tree Collapse file tree 2 files changed +40
-6
lines changed Original file line number Diff line number Diff line change @@ -4,9 +4,10 @@ https://neetcode.io/roadmap
4
4
5
5
### Leetcode
6
6
7
- | # | Title | Difficulty | Solution |
8
- | --- | ----------------------------------------------------------------------- | ---------- | ---------------------------------------------------- |
9
- | 217 | [ Contains Duplicate] ( https://leetcode.com/problems/contains-duplicate/ ) | Easy | [ TypeScript] ( ./TypeScript/217.contains-duplicate.ts ) |
10
- | 242 | [ Valid Anagram] ( https://leetcode.com/problems/valid-anagram/ ) | Easy | [ TypeScript] ( ./TypeScript/242.valid-anagram.ts ) |
11
- | 242 | [ Two Sum] ( https://leetcode.com/problems/two-sum/ ) | Easy | [ TypeScript] ( ./TypeScript/1.two-sum.ts ) |
12
- | 49 | [ Group Anagrams] ( https://leetcode.com/problems/two-sum/ ) | Medium | [ TypeScript] ( ./TypeScript/49.group-anagrams.ts ) |
7
+ | # | Title | Difficulty | Solution |
8
+ | --- | --------------------------------------------------------------------------------- | ---------- | --------------------------------------------------------- |
9
+ | 217 | [ Contains Duplicate] ( https://leetcode.com/problems/contains-duplicate/ ) | Easy | [ TypeScript] ( ./TypeScript/217.contains-duplicate.ts ) |
10
+ | 242 | [ Valid Anagram] ( https://leetcode.com/problems/valid-anagram/ ) | Easy | [ TypeScript] ( ./TypeScript/242.valid-anagram.ts ) |
11
+ | 242 | [ Two Sum] ( https://leetcode.com/problems/two-sum/ ) | Easy | [ TypeScript] ( ./TypeScript/1.two-sum.ts ) |
12
+ | 49 | [ Group Anagrams] ( https://leetcode.com/problems/two-sum/ ) | Medium | [ TypeScript] ( ./TypeScript/49.group-anagrams.ts ) |
13
+ | 347 | [ Top K Frequent Elements] ( https://leetcode.com/problems/top-k-frequent-elements/ ) | Medium | [ TypeScript] ( ./TypeScript/347.top-k-frequent-elements.ts ) |
Original file line number Diff line number Diff line change
1
+ function topKFrequent ( nums : number [ ] , k : number ) : number [ ] {
2
+ const map = new Map < number , number > ( ) ;
3
+
4
+ nums . forEach ( ( n ) => {
5
+ const frequency = map . get ( n ) ?? 0 ;
6
+
7
+ map . set ( n , frequency + 1 ) ;
8
+ } ) ;
9
+
10
+ return Array . from ( map )
11
+ . sort ( ( [ n1 , f1 ] , [ n2 , f2 ] ) => f2 - f1 )
12
+ . slice ( 0 , k )
13
+ . map ( ( [ n , f ] ) => n ) ;
14
+ }
15
+
16
+ function topKFrequent2 ( nums : number [ ] , k : number ) : number [ ] {
17
+ const map = new Map < number , number > ( ) ;
18
+ const count : number [ ] [ ] = new Array ( nums . length + 1 ) . fill ( 0 ) . map ( ( ) => [ ] ) ;
19
+ const result : number [ ] = [ ] ;
20
+
21
+ nums . forEach ( ( n ) => map . set ( n , ( map . get ( n ) ?? 0 ) + 1 ) ) ;
22
+ map . forEach ( ( f , n ) => count [ f ] . push ( n ) ) ;
23
+
24
+ for ( let i = count . length - 1 ; i >= 1 ; i -- ) {
25
+ for ( let j = 0 ; j < count [ i ] . length ; j ++ ) {
26
+ result . push ( count [ i ] [ j ] ) ;
27
+
28
+ if ( result . length === k ) return result ;
29
+ }
30
+ }
31
+
32
+ return result ;
33
+ }
You can’t perform that action at this time.
0 commit comments