Skip to content

Commit d06ad71

Browse files
my solution to 1636
1 parent fe39758 commit d06ad71

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

problems/1636/jeremymanning.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
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!

0 commit comments

Comments
 (0)