返回介绍

solution / 2100-2199 / 2174.Remove All Ones With Row and Column Flips II / README_EN

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

2174. Remove All Ones With Row and Column Flips II

中文文档

Description

You are given a 0-indexed m x n binary matrix grid.

In one operation, you can choose any i and j that meet the following conditions:

  • 0 <= i < m
  • 0 <= j < n
  • grid[i][j] == 1

and change the values of all cells in row i and column j to zero.

Return _the minimum number of operations needed to remove all _1_'s from _grid_._

 

Example 1:

Input: grid = [[1,1,1],[1,1,1],[0,1,0]]
Output: 2
Explanation:
In the first operation, change all cell values of row 1 and column 1 to zero.
In the second operation, change all cell values of row 0 and column 0 to zero.

Example 2:

Input: grid = [[0,1,0],[1,0,1],[0,1,0]]
Output: 2
Explanation:
In the first operation, change all cell values of row 1 and column 0 to zero.
In the second operation, change all cell values of row 2 and column 1 to zero.
Note that we cannot perform an operation using row 1 and column 1 because grid[1][1] != 1.

Example 3:

Input: grid = [[0,0],[0,0]]
Output: 0
Explanation:
There are no 1's to remove so return 0.

 

Constraints:

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

Solutions

Solution 1

class Solution:
  def removeOnes(self, grid: List[List[int]]) -> int:
    m, n = len(grid), len(grid[0])
    state = sum(1 << (i * n + j) for i in range(m) for j in range(n) if grid[i][j])
    q = deque([state])
    vis = {state}
    ans = 0
    while q:
      for _ in range(len(q)):
        state = q.popleft()
        if state == 0:
          return ans
        for i in range(m):
          for j in range(n):
            if grid[i][j] == 0:
              continue
            nxt = state
            for r in range(m):
              nxt &= ~(1 << (r * n + j))
            for c in range(n):
              nxt &= ~(1 << (i * n + c))
            if nxt not in vis:
              vis.add(nxt)
              q.append(nxt)
      ans += 1
    return -1
class Solution {
  public int removeOnes(int[][] grid) {
    int m = grid.length, n = grid[0].length;
    int state = 0;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 1) {
          state |= 1 << (i * n + j);
        }
      }
    }
    Deque<Integer> q = new ArrayDeque<>();
    q.offer(state);
    Set<Integer> vis = new HashSet<>();
    vis.add(state);
    int ans = 0;
    while (!q.isEmpty()) {
      for (int k = q.size(); k > 0; --k) {
        state = q.poll();
        if (state == 0) {
          return ans;
        }
        for (int i = 0; i < m; ++i) {
          for (int j = 0; j < n; ++j) {
            if (grid[i][j] == 0) {
              continue;
            }
            int nxt = state;
            for (int r = 0; r < m; ++r) {
              nxt &= ~(1 << (r * n + j));
            }
            for (int c = 0; c < n; ++c) {
              nxt &= ~(1 << (i * n + c));
            }
            if (!vis.contains(nxt)) {
              vis.add(nxt);
              q.offer(nxt);
            }
          }
        }
      }
      ++ans;
    }
    return -1;
  }
}
class Solution {
public:
  int removeOnes(vector<vector<int>>& grid) {
    int m = grid.size(), n = grid[0].size();
    int state = 0;
    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        if (grid[i][j])
          state |= (1 << (i * n + j));
    queue<int> q{{state}};
    unordered_set<int> vis{{state}};
    int ans = 0;
    while (!q.empty()) {
      for (int k = q.size(); k > 0; --k) {
        state = q.front();
        q.pop();
        if (state == 0) return ans;
        for (int i = 0; i < m; ++i) {
          for (int j = 0; j < n; ++j) {
            if (grid[i][j] == 0) continue;
            int nxt = state;
            for (int r = 0; r < m; ++r) nxt &= ~(1 << (r * n + j));
            for (int c = 0; c < n; ++c) nxt &= ~(1 << (i * n + c));
            if (!vis.count(nxt)) {
              vis.insert(nxt);
              q.push(nxt);
            }
          }
        }
      }
      ++ans;
    }
    return -1;
  }
};
func removeOnes(grid [][]int) int {
  m, n := len(grid), len(grid[0])
  state := 0
  for i, row := range grid {
    for j, v := range row {
      if v == 1 {
        state |= 1 << (i*n + j)
      }
    }
  }
  q := []int{state}
  vis := map[int]bool{state: true}
  ans := 0
  for len(q) > 0 {
    for k := len(q); k > 0; k-- {
      state = q[0]
      if state == 0 {
        return ans
      }
      q = q[1:]
      for i, row := range grid {
        for j, v := range row {
          if v == 0 {
            continue
          }
          nxt := state
          for r := 0; r < m; r++ {
            nxt &= ^(1 << (r*n + j))
          }
          for c := 0; c < n; c++ {
            nxt &= ^(1 << (i*n + c))
          }
          if !vis[nxt] {
            vis[nxt] = true
            q = append(q, nxt)
          }
        }
      }
    }
    ans++
  }
  return -1
}

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

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

发布评论

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