Skip to content

Commit 31e48b6

Browse files
add 1636
1 parent bb42bdb commit 31e48b6

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1636|[Sort Array by Increasing Frequency](https://leetcode.com/problems/sort-array-by-increasing-frequency/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1636.java) ||Easy|Array, Sort|
1112
|1630|[Arithmetic Subarrays](https://leetcode.com/problems/arithmetic-subarrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1630.java) ||Medium|Sort|
1213
|1629|[Slowest Key](https://leetcode.com/problems/slowest-key/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1629.java) ||Easy|Array|
1314
|1626|[Best Team With No Conflicts](https://leetcode.com/problems/best-team-with-no-conflicts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1626.java) ||Medium|DP|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.TreeMap;
9+
10+
public class _1636 {
11+
public static class Solution1 {
12+
public int[] frequencySort(int[] nums) {
13+
Map<Integer, Integer> count = new HashMap<>();
14+
for (int num : nums) {
15+
count.put(num, count.getOrDefault(num, 0) + 1);
16+
}
17+
TreeMap<Integer, List<Integer>> map = new TreeMap<>();
18+
for (int num : count.keySet()) {
19+
int freq = count.get(num);
20+
if (!map.containsKey(freq)) {
21+
map.put(freq, new ArrayList<>());
22+
}
23+
List<Integer> list = map.get(freq);
24+
list.add(num);
25+
map.put(freq, list);
26+
}
27+
int[] result = new int[nums.length];
28+
int i = 0;
29+
for (int num : map.keySet()) {
30+
List<Integer> list = map.get(num);
31+
Collections.sort(list, Collections.reverseOrder());
32+
int k = num;
33+
for (int j = 0; j < list.size(); j++, k = num) {
34+
while (k-- > 0) {
35+
result[i++] = list.get(j);
36+
}
37+
}
38+
}
39+
return result;
40+
}
41+
}
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1636;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1636Test {
10+
private static _1636.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1636.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{1, 1, 2, 2, 2, 3};
21+
assertArrayEquals(new int[]{3, 1, 1, 2, 2, 2}, solution1.frequencySort(nums));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[]{-53, -53, 52, 52, 52, 52, -53, -53, 52, -53, 52, 52, 52, -53, 52, 52, -53, 52, -53, 52, -53, 52, 52, 52, 52, 52, 52, 52, 52, 52, -53, 52, -53, 52, -53, 52, 52, 52, -53, -53, 52, -53, 52, 52, 52, 52, -53, -53, -53, -53, -53, 52, 52, -53, 52, -53, 52, 52, 52};
27+
assertArrayEquals(new int[]{-53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, -53, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52}, solution1.frequencySort(nums));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)