aboutsummaryrefslogtreecommitdiff
path: root/score_after_flipping_matrix.py
diff options
context:
space:
mode:
authorjack <0x6A73@pm.me>2024-06-14 13:13:15 -0600
committerjack <0x6A73@pm.me>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /score_after_flipping_matrix.py
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'score_after_flipping_matrix.py')
-rw-r--r--score_after_flipping_matrix.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/score_after_flipping_matrix.py b/score_after_flipping_matrix.py
new file mode 100644
index 0000000..7f80f58
--- /dev/null
+++ b/score_after_flipping_matrix.py
@@ -0,0 +1,29 @@
+# 861. Score After Flipping Matrix
+
+"""
+given an 'm n' binary matrix 'grid'. a move consists of choosing any row or
+column and toggling each value in thatrow or column. every row of the matrix
+is interpreted as a binary number, and the score of the matrix is the sum of
+these numbers. return the highest possible score after making any number of
+moves, including zero.
+"""
+
+
+class Solution(object):
+ def matrixScore(self, grid):
+ """
+ :type grid: List[List[int]]
+ :rtype: int
+ """
+ m, n = len(grid), len(grid[0])
+ ans = (1 << n - 1) * m
+ for i in range(1, n):
+ curr = sum(grid[j][i] == grid[j][0] for j in range(m))
+ ans += max(curr, m - curr) * (1 << n - 1 - i)
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.matrixScore(grid=[[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 0, 0]]))
+ print(obj.matrixScore(grid=[[0]]))