Skip to content

Commit ddaeb0d

Browse files
Merge branch 'main' of https://github.com/contextlab/leetcode-solutions into main
2 parents edf12b7 + 4dc6a40 commit ddaeb0d

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Each day (ideally) we'll attempt the daily [leetcode](https://leetcode.com) prob
3131
| July 20, 2024 | [1605](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/description/?envType=daily-question) | [Click here](https://github.com/ContextLab/leetcode-solutions/tree/main/problems/1605) | 🟡 Medium |
3232
| July 21, 2024 | [2392](https://leetcode.com/problems/build-a-matrix-with-conditions/description/?envType=daily-question) | [Click here](https://github.com/ContextLab/leetcode-solutions/tree/main/problems/2392) | 🔴 Hard |
3333
| July 22, 2024 | [2418](https://leetcode.com/problems/sort-the-people/description/?envType=daily-question) | [Click here](https://github.com/ContextLab/leetcode-solutions/tree/main/problems/2418) | 🟢 Easy |
34+
| July 23, 2024 | [1636](https://leetcode.com/problems/sort-array-by-increasing-frequency/description/?envType=daily-question) | [Click here](https://github.com/ContextLab/leetcode-solutions/tree/main/problems/1636) | 🟢 Easy |
3435

3536
# Join our discussion!
3637

problems/1636/jeremymanning.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# [Problem 1636: Sort Array by Increasing Frequency](https://leetcode.com/problems/sort-array-by-increasing-frequency/description/?envType=daily-question)
2+
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]])]`
7+
8+
## Refining the problem, round 2 thoughts
9+
- No special cases to account for that I can think of...
10+
11+
## Attempted solution(s)
12+
```python
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]])]
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!
31+
32+
- Actually: Maybe we can save some memory by not re-storing the counts:
33+
```python
34+
class Solution:
35+
def frequencySort(self, nums: List[int]) -> List[int]:
36+
counts = {}
37+
for n in nums:
38+
if n in counts:
39+
counts[n] += 1
40+
else:
41+
counts[n] = 1
42+
43+
return [n for n in sorted(nums, key=lambda x: [counts[x], -x])]
44+
```
45+
46+
![Screenshot 2024-07-22 at 8 16 25 PM](https://github.com/user-attachments/assets/db39e587-23c4-4974-bcd6-7ca5ab2c48c0)
47+
48+
Nope, not helpful 🙃!

0 commit comments

Comments
 (0)