Skip to content

2174. Remove All Ones With Row and Column Flips II

Approach 1: Top-down

  • Time: $O(2^{mn} \cdot mn \cdot \max(m, n))$
  • Space: $O(2^{mn})$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
 public:
  int removeOnes(vector<vector<int>>& grid) {
    const int m = grid.size();
    const int n = grid[0].size();
    vector<int> mem(1 << m * n, INT_MAX);
    return removeOnes(encode(grid, m, n), m, n, mem);
  }

 private:
  // Returns the minimum number of operations to remove all 1s from the grid,
  // where `mask` is the bitmask of the state of the grid.
  int removeOnes(int mask, int m, int n, vector<int>& mem) {
    if (mask == 0)
      return 0;
    if (mem[mask] < INT_MAX)
      return mem[mask];

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        if (mask >> i * n + j & 1) {  // grid[i][j] == 1
          int nextMask = mask;
          // Set the cells in the same row with 0.
          for (int k = 0; k < n; ++k)
            nextMask &= ~(1 << i * n + k);
          // Set the cells in the same column with 0.
          for (int k = 0; k < m; ++k)
            nextMask &= ~(1 << k * n + j);
          mem[mask] = min(mem[mask], 1 + removeOnes(nextMask, m, n, mem));
        }

    return mem[mask];
  }

  int encode(const vector<vector<int>>& grid, int m, int n) {
    int encoded = 0;
    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        if (grid[i][j])
          encoded |= 1 << i * n + j;
    return encoded;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
  public int removeOnes(int[][] grid) {
    final int m = grid.length;
    final int n = grid[0].length;
    int[] mem = new int[1 << m * n];
    Arrays.fill(mem, Integer.MAX_VALUE);
    return removeOnes(encode(grid, m, n), m, n, mem);
  }

  // Returns the minimum number of operations to remove all 1s from the grid,
  // where `mask` is the bitmask of the state of the grid.
  private int removeOnes(int mask, int m, int n, int[] mem) {
    if (mask == 0)
      return 0;
    if (mem[mask] < Integer.MAX_VALUE)
      return mem[mask];

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        if ((mask >> i * n + j & 1) == 1) { // grid[i][j] == 1
          int nextMask = mask;
          // Set the cells in the same row with 0.
          for (int k = 0; k < n; ++k)
            nextMask &= ~(1 << i * n + k);
          // Set the cells in the same column with 0.
          for (int k = 0; k < m; ++k)
            nextMask &= ~(1 << k * n + j);
          mem[mask] = Math.min(mem[mask], 1 + removeOnes(nextMask, m, n, mem));
        }

    return mem[mask];
  }

  private int encode(int[][] grid, int m, int n) {
    int encoded = 0;
    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        if (grid[i][j] == 1)
          encoded |= 1 << i * n + j;
    return encoded;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution:
  def removeOnes(self, grid: list[list[int]]) -> int:
    m = len(grid)
    n = len(grid[0])

    @functools.lru_cache(None)
    def dp(mask: int) -> int:
      """
      Returns the minimum number of operations to remove all 1s from the grid,
      where `mask` is the bitmask of the state of the grid.
      """
      if mask == 0:
        return 0
      ans = math.inf
      for i in range(m):
        for j in range(n):
          if mask >> i * n + j & 1:  # grid[i][j] == 1
            nextMask = mask
            for k in range(n):  # Set the cells in the same row with 0.
              nextMask &= ~(1 << i * n + k)
            for k in range(m):  # Set the cells in the same column with 0.
              nextMask &= ~(1 << k * n + j)
            ans = min(ans, 1 + dp(nextMask))
      return ans

    return dp(self.encode(grid, m, n))

  def encode(self, grid: list[list[int]], m: int, n: int) -> int:
    encoded = 0
    for i in range(m):
      for j in range(n):
        if grid[i][j]:
          encoded |= 1 << i * n + j
    return encoded

Approach 2: Bottom-up

  • Time: $O(2^{mn} \cdot mn \cdot \max(m, n))$
  • Space: $O(2^{mn})$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
 public:
  int removeOnes(vector<vector<int>>& grid) {
    const int m = grid.size();
    const int n = grid[0].size();
    const int maxMask = 1 << m * n;
    // dp[i] := the minimum number of operations to remove all 1s from the grid,
    // where `i` is the bitmask of the state of the grid
    vector<int> dp(maxMask, INT_MAX / 2);
    dp[0] = 0;

    for (int mask = 0; mask < maxMask; ++mask)
      for (int i = 0; i < m; ++i)
        for (int j = 0; j < n; ++j)
          if (grid[i][j] == 1) {
            int nextMask = mask;
            // Set the cells in the same row with 0.
            for (int k = 0; k < n; ++k)
              nextMask &= ~(1 << i * n + k);
            // Set the cells in the same column with 0.
            for (int k = 0; k < m; ++k)
              nextMask &= ~(1 << k * n + j);
            dp[mask] = min(dp[mask], 1 + dp[nextMask]);
          }

    return dp[encode(grid, m, n)];
  }

 private:
  int encode(const vector<vector<int>>& grid, int m, int n) {
    int encoded = 0;
    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        if (grid[i][j])
          encoded |= 1 << i * n + j;
    return encoded;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
  public int removeOnes(int[][] grid) {
    final int m = grid.length;
    final int n = grid[0].length;
    final int maxMask = 1 << m * n;
    // dp[i] := the minimum number of operations to remove all 1s from the grid,
    // where `i` is the bitmask of the state of the grid
    int[] dp = new int[1 << m * n];
    Arrays.fill(dp, Integer.MAX_VALUE / 2);
    dp[0] = 0;

    for (int mask = 0; mask < maxMask; ++mask)
      for (int i = 0; i < m; ++i)
        for (int j = 0; j < n; ++j)
          if (grid[i][j] == 1) {
            int nextMask = mask;
            // Set the cells in the same row with 0.
            for (int k = 0; k < n; ++k)
              nextMask &= ~(1 << i * n + k);
            // Set the cells in the same column with 0.
            for (int k = 0; k < m; ++k)
              nextMask &= ~(1 << k * n + j);
            dp[mask] = Math.min(dp[mask], 1 + dp[nextMask]);
          }

    return dp[encode(grid, m, n)];
  }

  private int encode(int[][] grid, int m, int n) {
    int encoded = 0;
    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        if (grid[i][j] == 1)
          encoded |= 1 << i * n + j;
    return encoded;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution:
  def removeOnes(self, grid: list[list[int]]) -> int:
    m = len(grid)
    n = len(grid[0])
    maxMask = 1 << m * n
    # dp[i] := the minimum number of operations to remove all 1s from the grid,
    # where `i` is the bitmask of the state of the grid
    dp = [math.inf] * maxMask
    dp[0] = 0

    for mask in range(maxMask):
      for i in range(m):
        for j in range(n):
          if grid[i][j] == 1:
            nextMask = mask
            # Set the cells in the same row with 0.
            for k in range(n):
              nextMask &= ~(1 << i * n + k)
            # Set the cells in the same column with 0.
            for k in range(m):
              nextMask &= ~(1 << k * n + j)
            dp[mask] = min(dp[mask], 1 + dp[nextMask])

    return dp[self.encode(grid, m, n)]

  def encode(self, grid: list[list[int]], m: int, n: int) -> int:
    encoded = 0
    for i in range(m):
      for j in range(n):
        if grid[i][j]:
          encoded |= 1 << i * n + j
    return encoded