java: add «861. Score After Flipping Matrix»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
0f5f897414
commit
7675c66d38
1 changed files with 22 additions and 0 deletions
22
java/score-after-flipping-matrix.java
Normal file
22
java/score-after-flipping-matrix.java
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
class Solution {
|
||||||
|
public int matrixScore(int[][] grid) {
|
||||||
|
int rows = grid.length;
|
||||||
|
int cols = grid[0].length;
|
||||||
|
|
||||||
|
int score = rows * (1 << (cols - 1));
|
||||||
|
|
||||||
|
for (int x = 1; x < cols; ++x) {
|
||||||
|
int same = 0;
|
||||||
|
for (int y = 0; y < rows; ++y) {
|
||||||
|
if (grid[y][x] == grid[y][0]) {
|
||||||
|
++same;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
same = Math.max(same, rows - same);
|
||||||
|
score += same * (1 << (cols - x - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue