Skip to content

Commit 2f628e0

Browse files
committed
added Score After Flipping Matrix (Medium)
1 parent 97195eb commit 2f628e0

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package Medium.ScoreAfterFlippingMatrix;
2+
3+
public class Driver {
4+
public static void main(String[] args) {
5+
Solution sol = new Solution();
6+
int[][] input = { { 0, 0, 1, 1 }, { 1, 0, 1, 0 }, { 1, 1, 0, 0 } };
7+
System.out.println(sol.matrixScore(input));
8+
}
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Score After Flipping Matrix
2+
3+
[Leetcode Link](https://leetcode.com/problems/score-after-flipping-matrix/)
4+
5+
## Problem:
6+
7+
We have a two dimensional matrix `A` where each value is `0` or `1`.
8+
9+
A move consists of choosing any row or column, and toggling each value in that row or column: changing all `0`s to `1`s, and all `1`s to `0`s.
10+
11+
After making any number of moves, every row of this matrix is interpreted as a binary number, and the score of the matrix is the sum of these numbers.
12+
13+
Return the highest possible score.
14+
15+
## Example:
16+
17+
```
18+
Input: [[0,0,1,1],[1,0,1,0],[1,1,0,0]]
19+
Output: 39
20+
Explanation:
21+
Toggled to [[1,1,1,1],[1,0,0,1],[1,1,1,1]].
22+
0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39
23+
```
24+
25+
## Note:
26+
27+
- 1 <= A.length <= 20
28+
- 1 <= A[0].length <= 20
29+
- A[i][j] is 0 or 1.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package Medium.ScoreAfterFlippingMatrix;
2+
3+
import java.util.Arrays;
4+
5+
public class Solution {
6+
public int matrixScore(int[][] A) {
7+
int rowLength = A.length;
8+
int colLength = A[0].length;
9+
int sum = 0;
10+
// 1. flip to higher value per row (highest value is binary that begins with
11+
// '1')
12+
for (int[] row : A) {
13+
if (row[0] == 0) {
14+
flipRow(row);
15+
}
16+
}
17+
System.out.println("After row flipping: " + Arrays.deepToString(A));
18+
// 2. check by columns, flip to max the number of 1's
19+
// basically, if there are more zeros in the column, flip to make more 1's, if
20+
// equal number of zeros and ones, then no effect at all (as bit-wise, it's like
21+
// subtract 4 and add 4)
22+
for (int j = 0; j < colLength; j++) {
23+
int numZero = 0;
24+
for (int[] row : A) {
25+
if (row[j] == 0)
26+
numZero++;
27+
}
28+
if (numZero > rowLength / 2) {
29+
flipCol(A, j);
30+
}
31+
}
32+
System.out.println("After col flipping: " + Arrays.deepToString(A));
33+
// 3. Lastly convert final array into binary and return sum
34+
for (int[] row : A) {
35+
sum += binaryArray(row);
36+
}
37+
return sum;
38+
}
39+
40+
// helper function to flip row
41+
private void flipRow(int[] row) {
42+
for (int i = 0; i < row.length; i++) {
43+
row[i] = row[i] == 0 ? 1 : 0;
44+
}
45+
}
46+
47+
// helper function to flip col
48+
private void flipCol(int[][] A, int colIndex) {
49+
for (int[] row : A) {
50+
row[colIndex] = row[colIndex] == 0 ? 1 : 0;
51+
}
52+
}
53+
54+
// helper function to convert 1-d array to binary then to a decimal number
55+
private int binaryArray(int[] arr) {
56+
int decimal = 0;
57+
int power = 0;
58+
for (int i = arr.length - 1; i >= 0; i--) {
59+
decimal += arr[i] * Math.pow(2, power);
60+
power++;
61+
}
62+
return decimal;
63+
}
64+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Languages used: Java and Python
8282
- [Count Square Submatrices with All Ones](Medium/CountSquareWithOnes)
8383
- [Divide Array is Sets of K Consecutive Numbers](Medium/DivideArrayInKConsecNums)
8484
- [Perfect Squares](Medium/PerfectSquares)
85+
- [Score After Flipping Matrix](Medium/ScoreAfterFlippingMatrix)
8586
- Hard
8687
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)
8788
- [Reducing Dishes](Hard/ReducingDishes)

0 commit comments

Comments
 (0)