Skip to content

Commit 2d86343

Browse files
authored
Merge pull request #2002 from Saipradyumnagoud/main
Added 861-score-after-flipping-matrix
2 parents 9bf6be6 + 8392547 commit 2d86343

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
---
2+
id: score-after-flipping-matrix
3+
title: Score After Flipping Matrix
4+
level: medium
5+
sidebar_label: Score After Flipping Matrix
6+
tags:
7+
- Array
8+
- Greedy
9+
- Matrix
10+
- Java
11+
description: "This document provides solutions for the Score After Flipping Matrix problem."
12+
---
13+
14+
## Problem Statement
15+
16+
You are given an `m x n` binary matrix `grid`.
17+
18+
A move consists of choosing any row or column and toggling each value in that row or column (i.e., changing all 0's to 1's, and all 1's to 0's).
19+
20+
Every row of the matrix is interpreted as a binary number, and the score of the matrix is the sum of these numbers.
21+
22+
Return the highest possible score after making any number of moves (including zero moves).
23+
24+
**Example 1:**
25+
26+
Input: `grid = [[0,0,1,1],[1,0,1,0],[1,1,0,0]]`
27+
28+
Output: `39`
29+
30+
Explanation: `0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39`
31+
32+
**Example 2:**
33+
34+
Input: `grid = [[0]]`
35+
36+
Output: `1`
37+
38+
**Constraints:**
39+
40+
- `m == grid.length`
41+
- `n == grid[i].length`
42+
- `1 <= m, n <= 20`
43+
- `grid[i][j]` is `0` or `1`.
44+
45+
## Solutions
46+
47+
### Approach
48+
49+
To maximize the score, follow these steps:
50+
51+
1. **Row Flipping:**
52+
- Ensure that each row starts with a `1` by flipping rows where the first element is `0`.
53+
54+
2. **Column Flipping:**
55+
- For each column, if the number of `0`s exceeds the number of `1`s, flip the column to maximize the number of `1`s in that column.
56+
57+
3. **Calculate Score:**
58+
- Convert each row from binary to decimal and sum these values to get the final score.
59+
60+
### Java
61+
62+
```java
63+
class Solution {
64+
public int matrixScore(int[][] grid) {
65+
int m = grid.length;
66+
int n = grid[0].length;
67+
int i = 0, j = 0;
68+
69+
for (i = 0; i < m; i++) {
70+
if (grid[i][0] == 0) {
71+
flipRow(grid, i);
72+
}
73+
}
74+
75+
for (j = 0; j < n; j++) {
76+
if (searchColZeroes(grid, j) > searchColOnes(grid, j)) {
77+
flipCol(grid, j);
78+
}
79+
}
80+
81+
int sum = 0;
82+
for (i = 0; i < grid.length; i++) {
83+
int count = 0;
84+
int temp = grid[0].length - 1;
85+
for (j = 0; j < grid[0].length; j++) {
86+
count += (int) (grid[i][j] * Math.pow(2, temp--));
87+
}
88+
sum += count;
89+
}
90+
return sum;
91+
}
92+
93+
public int searchColZeroes(int[][] grid, int j) {
94+
int count = 0;
95+
for (int temp = 0; temp < grid.length; temp++) {
96+
if (grid[temp][j] == 0) {
97+
count++;
98+
}
99+
}
100+
return count;
101+
}
102+
103+
public int searchColOnes(int[][] grid, int j) {
104+
int count = 0;
105+
for (int temp = 0; temp < grid.length; temp++) {
106+
if (grid[temp][j] == 1) {
107+
count++;
108+
}
109+
}
110+
return count;
111+
}
112+
113+
public void flipCol(int[][] grid, int j) {
114+
for (int temp = 0; temp < grid.length; temp++) {
115+
grid[temp][j] = (grid[temp][j] == 0) ? 1 : 0;
116+
}
117+
}
118+
119+
public void flipRow(int[][] grid, int i) {
120+
for (int temp = 0; temp < grid[0].length; temp++) {
121+
grid[i][temp] = (grid[i][temp] == 0) ? 1 : 0;
122+
}
123+
}
124+
}
125+
```
126+
127+
### Python
128+
```Python
129+
class Solution:
130+
def matrixScore(self, grid: List[List[int]]) -> int:
131+
m = len(grid)
132+
n = len(grid[0])
133+
134+
# Step 1: Ensure all rows start with '1' by flipping rows where grid[i][0] == 0
135+
for i in range(m):
136+
if grid[i][0] == 0:
137+
self.flipRow(grid, i)
138+
139+
# Step 2: Ensure each column has more '1's than '0's by flipping columns if necessary
140+
for j in range(n):
141+
if self.searchColZeroes(grid, j) > self.searchColOnes(grid, j):
142+
self.flipCol(grid, j)
143+
144+
# Step 3: Calculate the matrix score
145+
score = 0
146+
for i in range(m):
147+
row_sum = 0
148+
for j in range(n):
149+
row_sum += grid[i][j] * (1 << (n - 1 - j)) # Equivalent to grid[i][j] * 2^(n-1-j)
150+
score += row_sum
151+
152+
return score
153+
154+
def searchColZeroes(self, grid: List[List[int]], j: int) -> int:
155+
count = 0
156+
for i in range(len(grid)):
157+
if grid[i][j] == 0:
158+
count += 1
159+
return count
160+
161+
def searchColOnes(self, grid: List[List[int]], j: int) -> int:
162+
count = 0
163+
for i in range(len(grid)):
164+
if grid[i][j] == 1:
165+
count += 1
166+
return count
167+
168+
def flipCol(self, grid: List[List[int]], j: int) -> None:
169+
for i in range(len(grid)):
170+
grid[i][j] = 1 - grid[i][j]
171+
172+
def flipRow(self, grid: List[List[int]], i: int) -> None:
173+
for j in range(len(grid[0])):
174+
grid[i][j] = 1 - grid[i][j]
175+
176+
```

0 commit comments

Comments
 (0)