Skip to content

Commit 571b05e

Browse files
add 3240
1 parent 8840b7b commit 571b05e

File tree

3 files changed

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

3 files changed

+102
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
33
| 3241 | [Time Taken to Mark All Nodes](https://leetcode.com/problems/time-taken-to-mark-all-nodes/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3241.java) | | Hard |
4+
| 3240 | [Minimum Number of Flips to Make Binary Grid Palindromic II](https://leetcode.com/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3240.java) | | Medium |
45
| 3239 | [Minimum Number of Flips to Make Binary Grid Palindromic I](https://leetcode.com/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3239.java) | | Easy |
56
| 3238 | [Find the Number of Winning Players](https://leetcode.com/problems/find-the-number-of-winning-players/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3238.java) | | Easy |
67
| 3237 | [Alt and Tab Simulation](https://leetcode.com/problems/alt-and-tab-simulation/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3237.java) | | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
public class _3240 {
4+
public static class Solution1 {
5+
/**
6+
* Credit: https://leetcode.com/problems/minimum-number-of-flips-to-make-binary-grid-palindromic-ii/solutions/5580937/java-o-m-n/
7+
*/
8+
public int minFlips(int[][] grid) {
9+
int m = grid.length;
10+
int n = grid[0].length;
11+
int ans = 0;
12+
for (int i = 0; i < m / 2; i++) {
13+
for (int j = 0; j < n / 2; j++) {
14+
int cnt = 0;
15+
cnt += grid[i][j];
16+
cnt += grid[m - i - 1][j];
17+
cnt += grid[i][n - j - 1];
18+
cnt += grid[m - i - 1][n - j - 1];
19+
ans += Math.min(cnt, 4 - cnt);
20+
}
21+
}
22+
int diff = 0;
23+
int p0 = 0;
24+
int p1 = 0;
25+
//process if there's odd number of rows
26+
if (m % 2 == 1) {
27+
for (int j = 0; j < n / 2; j++) {
28+
if (grid[m / 2][j] != grid[m / 2][n - j - 1]) {
29+
diff++;
30+
} else {
31+
if (grid[m / 2][j] == 0) {
32+
p0++;
33+
} else {
34+
p1++;
35+
}
36+
}
37+
}
38+
}
39+
//process if there's odd number of columns
40+
if (n % 2 == 1) {
41+
for (int i = 0; i < m / 2; i++) {
42+
if (grid[i][n / 2] != grid[m - i - 1][n / 2]) {
43+
diff++;
44+
} else {
45+
if (grid[i][n / 2] == 0) {
46+
p0++;
47+
} else {
48+
p1++;
49+
}
50+
}
51+
}
52+
}
53+
54+
if (m % 2 == 1 && n % 2 == 1) {
55+
if (grid[m / 2][n / 2] == 1) {
56+
ans++;
57+
}
58+
}
59+
60+
int ans1;
61+
if (diff % 2 == p1 % 2) {
62+
ans1 = diff;
63+
} else {
64+
if (diff % 2 == 0) {
65+
if (diff == 0) {
66+
ans1 = 2;
67+
} else {
68+
ans1 = diff;
69+
}
70+
} else {
71+
ans1 = diff;
72+
}
73+
}
74+
75+
return ans + ans1;
76+
}
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions.fourththousand._3240;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class _3240Test {
11+
private static _3240.Solution1 solution1;
12+
13+
@BeforeEach
14+
public void setup() {
15+
solution1 = new _3240.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(3, solution1.minFlips(CommonUtils
21+
.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,0,0],[0,1,0],[0,0,1]")));
22+
}
23+
}

0 commit comments

Comments
 (0)