File tree Expand file tree Collapse file tree 1 file changed +21
-2
lines changed Expand file tree Collapse file tree 1 file changed +21
-2
lines changed Original file line number Diff line number Diff line change 11# [ Problem 1636: Sort Array by Increasing Frequency] ( https://leetcode.com/problems/sort-array-by-increasing-frequency/description/?envType=daily-question )
22
33## 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]])] `
47
58## Refining the problem, round 2 thoughts
9+ - No special cases to account for that I can think of...
610
711## Attempted solution(s)
812``` 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 ]])]
1124```
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