Skip to content

Commit f92331b

Browse files
add 4065
1 parent d4b1105 commit f92331b

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

  • paginated_contents/algorithms/5th_thousand
  • src

‎paginated_contents/algorithms/5th_thousand/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|--------------------------------------|------------|----------------------------------------------------------------------
3+
| 4065 | [Rearrange Array by Removing Distinct Values](https://leetcode.com/problems/rearrange-array-by-removing-distinct-values/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fifththousand/_4065.java) | | Easy |
34
| 4056 | [Number of Intersecting Interval Pairs I](https://leetcode.com/problems/number-of-intersecting-interval-pairs-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fifththousand/_4056.java) | | Easy |
45
| 4052 | [Cyclically Shift Rows and Columns](https://leetcode.com/problems/cyclically-shift-rows-and-columns/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fifththousand/_4052.java) | | Easy |
56
| 4048 | [Count Values With Equally Spaced Occurrences I](https://leetcode.com/problems/count-values-with-equally-spaced-occurrences-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fifththousand/_4048.java) | | Easy |
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder.solutions.fifththousand;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.PriorityQueue;
6+
7+
public class _4065 {
8+
public static class Solution1 {
9+
public int[] rearrangeArray(int[] nums) {
10+
Map<Integer, Integer> map = new HashMap<>();
11+
for (int key : nums) {
12+
map.put(key, map.getOrDefault(key, 0) + 1);
13+
}
14+
PriorityQueue<int[]> pq =
15+
new PriorityQueue<>(
16+
(a, b) -> {
17+
if (a[0] != b[0]) {
18+
return a[0] - b[0];
19+
}
20+
return a[1] - b[1];
21+
});
22+
for (int key : map.keySet()) {
23+
pq.offer(new int[] {key, map.get(key)});
24+
}
25+
int[] res = new int[nums.length];
26+
int i = 0;
27+
PriorityQueue<int[]> pq2 =
28+
new PriorityQueue<>(
29+
(a, b) -> {
30+
if (a[0] != b[0]) {
31+
return a[0] - b[0];
32+
}
33+
return a[1] - b[1];
34+
});
35+
while (!pq.isEmpty()) {
36+
int size = pq.size();
37+
for (int index = 0; index < size && i < nums.length; index++) {
38+
int[] cur = pq.poll();
39+
res[i++] = cur[0];
40+
if (cur[1] > 1) {
41+
pq2.offer(new int[] {cur[0], cur[1] - 1});
42+
}
43+
}
44+
if (!pq2.isEmpty()) {
45+
pq.addAll(pq2);
46+
pq2.clear();
47+
}
48+
}
49+
return res;
50+
}
51+
}
52+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder.fifththousand;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import com.fishercoder.solutions.fifththousand._4065;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class _4065Test {
10+
private _4065.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _4065.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(
20+
new int[] {1, 2, 3, 1, 3, 3},
21+
solution1.rearrangeArray(new int[] {3, 1, 3, 2, 1, 3}));
22+
}
23+
}

0 commit comments

Comments
 (0)