Skip to content

Commit 8c536fb

Browse files
add 861
1 parent 26cf7ae commit 8c536fb

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+
package com.fishercoder.solutions;
2+
3+
public class _861 {
4+
public static class Solution1 {
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+
public int matrixScore(int[][] A) {
14+
int m = A.length;
15+
int n = A[0].length;
16+
for (int i = 0; i < m; i++) {
17+
if (A[i][0] == 0) {
18+
for (int j = 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 (int j = 1; j < n; j++) {
28+
int ones = 0;
29+
for (int i = 0; i < m; i++) {
30+
if (A[i][j] == 1) {
31+
ones++;
32+
}
33+
}
34+
if (ones <= m / 2) {
35+
for (int i = 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+
int result = 0;
45+
for (int i = 0; i < m; i++) {
46+
StringBuilder sb = new StringBuilder();
47+
for (int j = 0; j < n; j++) {
48+
sb.append(A[i][j]);
49+
}
50+
result += Integer.parseInt(sb.toString(), 2);
51+
}
52+
return result;
53+
}
54+
}
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._861;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _861Test {
11+
private static _861.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _861.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy