Skip to content

Commit dc1249b

Browse files
add 3224
1 parent af2c430 commit dc1249b

File tree

3 files changed

+106
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+106
-0
lines changed

Diff for: paginated_contents/algorithms/4th_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3224 | [Minimum Array Changes to Make Differences Equal](https://leetcode.com/problems/minimum-array-changes-to-make-differences-equal/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3224.java) | | Medium |
34
| 3223 | [Minimum Length of String After Operations](https://leetcode.com/problems/minimum-length-of-string-after-operations/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3223.java) | | Medium |
45
| 3222 | [Find the Winning Player in Coin Game](https://leetcode.com/problems/find-the-winning-player-in-coin-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3222.java) | | Easy |
56
| 3219 | [Minimum Cost for Cutting Cake II](https://leetcode.com/problems/minimum-cost-for-cutting-cake-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3219.java) | | Hard | Greedy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.fishercoder.solutions.fourththousand;
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+
9+
public class _3224 {
10+
public static class Solution1 {
11+
/**
12+
* My completely original solution during the contest.
13+
*/
14+
public int minChanges(int[] nums, int k) {
15+
//compute the frequency of each diff
16+
Map<Integer, Integer> map = new HashMap<>();
17+
for (int i = 0; i < nums.length / 2; i++) {
18+
int diff = Math.abs(nums[nums.length - i - 1] - nums[i]);
19+
map.put(diff, map.getOrDefault(diff, 0) + 1);
20+
}
21+
List<int[]> list = new ArrayList<>();
22+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
23+
list.add(new int[]{entry.getKey(), entry.getValue()});
24+
}
25+
//sort them by their frequency
26+
Collections.sort(list, (a, b) -> b[1] - a[1]);
27+
List<int[]> modes = new ArrayList<>();
28+
modes.add(list.get(0));
29+
int i = 1;
30+
//in case there are ties for the same mode
31+
while (i < list.size() && list.get(i)[1] == list.get(0)[1]) {
32+
modes.add(list.get(i++));
33+
}
34+
//we'll take the second most frequent mode as well, otherwise, test case 4 won't pass
35+
if (i < list.size()) {
36+
modes.add(list.get(i));
37+
}
38+
int minChanges = nums.length;
39+
for (int[] mode : modes) {
40+
minChanges = Math.min(minChanges, computeChanges(mode[0], nums, k));
41+
}
42+
return minChanges;
43+
}
44+
45+
private int computeChanges(int mode, int[] nums, int k) {
46+
int changes = 0;
47+
for (int i = 0; i < nums.length / 2; i++) {
48+
int diff = Math.abs(nums[nums.length - i - 1] - nums[i]);
49+
if (diff != mode) {
50+
if (nums[nums.length - i - 1] > nums[i]) {
51+
if (k - nums[i] >= mode || nums[nums.length - i - 1] >= mode) {
52+
changes++;
53+
} else {
54+
changes += 2;
55+
}
56+
} else {
57+
if (k - nums[nums.length - i - 1] >= mode || nums[i] >= mode) {
58+
changes++;
59+
} else {
60+
changes += 2;
61+
}
62+
}
63+
}
64+
}
65+
return changes;
66+
}
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3224;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _3224Test {
10+
private static _3224.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3224.Solution1();
15+
}
16+
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(2, solution1.minChanges(new int[]{1, 0, 1, 2, 4, 3}, 4));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(2, solution1.minChanges(new int[]{18, 10, 14, 18, 17, 2, 11, 5}, 19));
26+
}
27+
28+
@Test
29+
public void test3() {
30+
assertEquals(4, solution1.minChanges(new int[]{9, 2, 7, 7, 8, 9, 1, 5, 1, 9, 4, 9, 4, 7}, 9));
31+
}
32+
33+
@Test
34+
public void test4() {
35+
assertEquals(7, solution1.minChanges(new int[]{1, 1, 1, 1, 0, 0, 0, 5, 4, 3, 19, 17, 16, 15, 15, 15, 19, 19, 19, 19}, 20));
36+
}
37+
}

0 commit comments

Comments
 (0)