Skip to content

3122. Minimum Number of Operations to Satisfy Conditions 👍

  • Time: $O(mn)$
  • Space: $O(10n) = O(n)$
 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
class Solution {
 public:
  int minimumOperations(vector<vector<int>>& grid) {
    const int m = grid.size();
    const int n = grid[0].size();
    vector<vector<int>> count(n, vector<int>(10));

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        ++count[j][grid[i][j]];

    vector<vector<int>> mem(n, vector<int>(10, -1));
    return minimumOperations(count, 0, 0, m, mem);
  }

 private:
  // Returns the number of minimum operations needed to make grid[:][j..n)
  // satisfy the conditions, where the (j - 1)-th column is filled with `prev`.
  int minimumOperations(const vector<vector<int>>& count, int j, int prev,
                        int m, vector<vector<int>>& mem) {
    if (j == count.size())
      return 0;
    if (mem[j][prev] != -1)
      return mem[j][prev];

    int res = INT_MAX;

    for (int num = 0; num < 10; ++num)
      if (j == 0 || num != prev)
        res = min(res, m - count[j][num] +
                           minimumOperations(count, j + 1, num, m, mem));

    return mem[j][prev] = res;
  }
};
 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
class Solution {
  public int minimumOperations(int[][] grid) {
    final int m = grid.length;
    final int n = grid[0].length;
    int[][] count = new int[n][10];

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        ++count[j][grid[i][j]];

    Integer[][] mem = new Integer[n][10];
    return minimumOperations(count, 0, 0, m, mem);
  }

  // Returns the number of minimum operations needed to make grid[:][j..n)
  // satisfy the conditions, where the (j - 1)-th column is filled with `prev`.
  private int minimumOperations(int[][] count, int j, int prev, int m, Integer[][] mem) {
    if (j == count.length)
      return 0;
    if (mem[j][prev] != null)
      return mem[j][prev];

    int res = Integer.MAX_VALUE;

    for (int num = 0; num < 10; ++num)
      if (j == 0 || num != prev)
        res = Math.min(res, m - count[j][num] + minimumOperations(count, j + 1, num, m, mem));

    return mem[j][prev] = res;
  }
}
 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
class Solution:
  def minimumOperations(self, grid: list[list[int]]) -> int:
    m = len(grid)
    n = len(grid[0])
    count = [[0] * 10 for _ in range(n)]

    for row in grid:
      for j, num in enumerate(row):
        count[j][num] += 1

    @functools.lru_cache(None)
    def dp(i: int, prev: int) -> int:
      """
      Returns the number of minimum operations needed to make grid[:][j..n)
      satisfy the conditions, where the (j - 1)-th column is filled with `prev`.
      """
      if i == n:
        return 0

      res = math.inf

      for num in range(10):
        if i == 0 or num != prev:
          res = min(res, m - count[i][num] + dp(i + 1, num))

      return res

    return dp(0, 0)