返回介绍

solution / 0800-0899 / 0861.Score After Flipping Matrix / README_EN

发布于 2024-06-17 01:03:33 字数 4972 浏览 0 评论 0 收藏 0

861. Score After Flipping Matrix

中文文档

Description

You are given an m x n binary matrix grid.

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).

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 moves)_.

 

Example 1:

Input: grid = [[0,0,1,1],[1,0,1,0],[1,1,0,0]]
Output: 39
Explanation: 0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39

Example 2:

Input: grid = [[0]]
Output: 1

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 20
  • grid[i][j] is either 0 or 1.

Solutions

Solution 1

class Solution:
  def matrixScore(self, grid: List[List[int]]) -> int:
    m, n = len(grid), len(grid[0])
    for i in range(m):
      if grid[i][0] == 0:
        for j in range(n):
          grid[i][j] ^= 1
    ans = 0
    for j in range(n):
      cnt = sum(grid[i][j] for i in range(m))
      ans += max(cnt, m - cnt) * (1 << (n - j - 1))
    return ans
class Solution {
  public int matrixScore(int[][] grid) {
    int m = grid.length, n = grid[0].length;
    for (int i = 0; i < m; ++i) {
      if (grid[i][0] == 0) {
        for (int j = 0; j < n; ++j) {
          grid[i][j] ^= 1;
        }
      }
    }
    int ans = 0;
    for (int j = 0; j < n; ++j) {
      int cnt = 0;
      for (int i = 0; i < m; ++i) {
        cnt += grid[i][j];
      }
      ans += Math.max(cnt, m - cnt) * (1 << (n - j - 1));
    }
    return ans;
  }
}
class Solution {
public:
  int matrixScore(vector<vector<int>>& grid) {
    int m = grid.size(), n = grid[0].size();
    for (int i = 0; i < m; ++i) {
      if (grid[i][0] == 0) {
        for (int j = 0; j < n; ++j) {
          grid[i][j] ^= 1;
        }
      }
    }
    int ans = 0;
    for (int j = 0; j < n; ++j) {
      int cnt = 0;
      for (int i = 0; i < m; ++i) {
        cnt += grid[i][j];
      }
      ans += max(cnt, m - cnt) * (1 << (n - j - 1));
    }
    return ans;
  }
};
func matrixScore(grid [][]int) int {
  m, n := len(grid), len(grid[0])
  for i := 0; i < m; i++ {
    if grid[i][0] == 0 {
      for j := 0; j < n; j++ {
        grid[i][j] ^= 1
      }
    }
  }
  ans := 0
  for j := 0; j < n; j++ {
    cnt := 0
    for i := 0; i < m; i++ {
      cnt += grid[i][j]
    }
    if cnt < m-cnt {
      cnt = m - cnt
    }
    ans += cnt * (1 << (n - j - 1))
  }
  return ans
}
function matrixScore(grid: number[][]): number {
  const m = grid.length;
  const n = grid[0].length;
  for (let i = 0; i < m; ++i) {
    if (grid[i][0] == 0) {
      for (let j = 0; j < n; ++j) {
        grid[i][j] ^= 1;
      }
    }
  }
  let ans = 0;
  for (let j = 0; j < n; ++j) {
    let cnt = 0;
    for (let i = 0; i < m; ++i) {
      cnt += grid[i][j];
    }
    ans += Math.max(cnt, m - cnt) * (1 << (n - j - 1));
  }
  return ans;
}
public class Solution {
  public int MatrixScore(int[][] grid) {
    int m = grid.Length, n = grid[0].Length;
    for (int i = 0; i < m; ++i) {
      if (grid[i][0] == 0) {
        for (int j = 0; j < n; ++j) {
          grid[i][j] ^= 1;
        }
      }
    }
    int ans = 0;
    for (int j = 0; j < n; ++j) {
      int cnt = 0;
      for (int i = 0; i < m; ++i) {
        if (grid[i][j] == 1) {
          ++cnt;
        }
      }
      ans += Math.Max(cnt, m - cnt) * (1 << (n - j - 1));
    }
    return ans;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文