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

Commit8c536fb

Browse files
add 861
1 parent26cf7ae commit8c536fb

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ _If you like this project, please leave me a star._ ★
428428
|870|[Advantage Shuffle](https://leetcode.com/problems/advantage-shuffle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_870.java) | |Medium|Array, Greedy
429429
|868|[Binary Gap](https://leetcode.com/problems/binary-gap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_868.java)||Easy|
430430
|867|[Transpose Matrix](https://leetcode.com/problems/transpose-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_867.java)||Easy|
431+
|861|[Score After Flipping Matrix](https://leetcode.com/problems/score-after-flipping-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_861.java) | |Medium| Greedy
431432
|860|[Lemonade Change](https://leetcode.com/problems/lemonade-change/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_860.java)||Easy|
432433
|859|[Buddy Strings](https://leetcode.com/problems/buddy-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_859.java)||Easy|
433434
|856|[Score of Parentheses](https://leetcode.com/problems/score-of-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_856.java) | |Medium| Stack, String
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
packagecom.fishercoder.solutions;
2+
3+
publicclass_861 {
4+
publicstaticclassSolution1 {
5+
/**
6+
* We can simply apply greedy methodology here.
7+
* 1. we check if the left most digits are ones or not, if it's a zero,
8+
* then we'll just flip this entire row, reason being the left most digit carries the biggest weight when interpreting this binary row/number;
9+
* 2. after step #1, we'll check column wise starting from the second column,
10+
* we'll count the number of ones in each column, if the number of ones in each column is less than or equal to half of the column length,
11+
* then flipping this column would make a bigger number
12+
*/
13+
publicintmatrixScore(int[][]A) {
14+
intm =A.length;
15+
intn =A[0].length;
16+
for (inti =0;i <m;i++) {
17+
if (A[i][0] ==0) {
18+
for (intj =0;j <n;j++) {
19+
if (A[i][j] ==0) {
20+
A[i][j] =1;
21+
}else {
22+
A[i][j] =0;
23+
}
24+
}
25+
}
26+
}
27+
for (intj =1;j <n;j++) {
28+
intones =0;
29+
for (inti =0;i <m;i++) {
30+
if (A[i][j] ==1) {
31+
ones++;
32+
}
33+
}
34+
if (ones <=m /2) {
35+
for (inti =0;i <m;i++) {
36+
if (A[i][j] ==1) {
37+
A[i][j] =0;
38+
}else {
39+
A[i][j] =1;
40+
}
41+
}
42+
}
43+
}
44+
intresult =0;
45+
for (inti =0;i <m;i++) {
46+
StringBuildersb =newStringBuilder();
47+
for (intj =0;j <n;j++) {
48+
sb.append(A[i][j]);
49+
}
50+
result +=Integer.parseInt(sb.toString(),2);
51+
}
52+
returnresult;
53+
}
54+
}
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._861;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticorg.junit.Assert.assertEquals;
9+
10+
publicclass_861Test {
11+
privatestatic_861.Solution1solution1;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_861.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
assertEquals(39,solution1.matrixScore(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,0,1,1],[1,0,1,0],[1,1,0,0]")));
21+
}
22+
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp