Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit571b05e

Browse files
add 3240
1 parent8840b7b commit571b05e

File tree

3 files changed

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

3 files changed

+102
-0
lines changed

‎paginated_contents/algorithms/4th_thousand/README.md

Lines changed: 1 addition & 0 deletions
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|
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
packagecom.fishercoder.solutions.fourththousand;
2+
3+
publicclass_3240 {
4+
publicstaticclassSolution1 {
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+
publicintminFlips(int[][]grid) {
9+
intm =grid.length;
10+
intn =grid[0].length;
11+
intans =0;
12+
for (inti =0;i <m /2;i++) {
13+
for (intj =0;j <n /2;j++) {
14+
intcnt =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+
intdiff =0;
23+
intp0 =0;
24+
intp1 =0;
25+
//process if there's odd number of rows
26+
if (m %2 ==1) {
27+
for (intj =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 (inti =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+
intans1;
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+
returnans +ans1;
76+
}
77+
}
78+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packagecom.fishercoder.fourththousand;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions.fourththousand._3240;
5+
importorg.junit.jupiter.api.BeforeEach;
6+
importorg.junit.jupiter.api.Test;
7+
8+
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
9+
10+
publicclass_3240Test {
11+
privatestatic_3240.Solution1solution1;
12+
13+
@BeforeEach
14+
publicvoidsetup() {
15+
solution1 =new_3240.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
assertEquals(3,solution1.minFlips(CommonUtils
21+
.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,0,0],[0,1,0],[0,0,1]")));
22+
}
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp