From 7675c66d385373058dfa5e14f045fe75e1c29366 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 13 May 2024 17:38:57 +0200 Subject: [PATCH] =?UTF-8?q?java:=20add=20=C2=AB861.=20Score=20After=20Flip?= =?UTF-8?q?ping=20Matrix=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- java/score-after-flipping-matrix.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 java/score-after-flipping-matrix.java diff --git a/java/score-after-flipping-matrix.java b/java/score-after-flipping-matrix.java new file mode 100644 index 0000000..e71fd83 --- /dev/null +++ b/java/score-after-flipping-matrix.java @@ -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; + } +}