File tree 1 file changed +21
-2
lines changed
1 file changed +21
-2
lines changed Original file line number Diff line number Diff line change 1
1
# [ Problem 1636: Sort Array by Increasing Frequency] ( https://leetcode.com/problems/sort-array-by-increasing-frequency/description/?envType=daily-question )
2
2
3
3
## Initial thoughts (stream-of-consciousness)
4
+ - We can make a hash table with the counts of each item
5
+ - Then we'll make an array ` x = [[counts[i], i] for i in nums] `
6
+ - Then we can return ` [n[1] for n in sorted(x, key=lambda x: [x[0], -x[1]])] `
4
7
5
8
## Refining the problem, round 2 thoughts
9
+ - No special cases to account for that I can think of...
6
10
7
11
## Attempted solution(s)
8
12
``` python
9
- class Solution : # paste your code here!
10
- ...
13
+ class Solution :
14
+ def frequencySort (self , nums : List[int ]) -> List[int ]:
15
+ counts = {}
16
+ for n in nums:
17
+ if n in counts:
18
+ counts[n] += 1
19
+ else :
20
+ counts[n] = 1
21
+
22
+ x = [[counts[i], i] for i in nums]
23
+ return [n[1 ] for n in sorted (x, key = lambda x : [x[0 ], - x[1 ]])]
11
24
```
25
+ - Given test cases pass
26
+ - Submitting...
27
+
28
+ ![ Screenshot 2024-07-22 at 8 13 33 PM] ( https://github.com/user-attachments/assets/2a22346c-4997-486e-a79d-8e473d4b78b3 )
29
+
30
+ Solved!
You can’t perform that action at this time.
0 commit comments