3402. Minimum Operations to Make Columns Strictly Increasing LeetCode Solution

In this guide, you will get 3402. Minimum Operations to Make Columns Strictly Increasing LeetCode Solution with the best time and space complexity. The solution to Minimum Operations to Make Columns Strictly Increasing problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Operations to Make Columns Strictly Increasing solution in C++
  4. Minimum Operations to Make Columns Strictly Increasing solution in Java
  5. Minimum Operations to Make Columns Strictly Increasing solution in Python
  6. Additional Resources
3402. Minimum Operations to Make Columns Strictly Increasing LeetCode Solution image

Problem Statement of Minimum Operations to Make Columns Strictly Increasing

You are given a m x n matrix grid consisting of non-negative integers.
In one operation, you can increment the value of any grid[i][j] by 1.
Return the minimum number of operations needed to make all columns of grid strictly increasing.

Example 1:

Input: grid = [[3,2],[1,3],[3,4],[0,1]]
Output: 15
Explanation:

To make the 0th column strictly increasing, we can apply 3 operations on grid[1][0], 2 operations on grid[2][0], and 6 operations on grid[3][0].
To make the 1st column strictly increasing, we can apply 4 operations on grid[3][1].

Example 2:

Input: grid = [[3,2,1],[2,1,0],[1,2,3]]
Output: 12
Explanation:

To make the 0th column strictly increasing, we can apply 2 operations on grid[1][0], and 4 operations on grid[2][0].
To make the 1st column strictly increasing, we can apply 2 operations on grid[1][1], and 2 operations on grid[2][1].
To make the 2nd column strictly increasing, we can apply 2 operations on grid[1][2].

See also  1394. Find Lucky Integer in an Array LeetCode Solution

Constraints:

m == grid.length
n == grid[i].length
1 <= m, n <= 50
0 <= grid[i][j] < 2500

Complexity Analysis

  • Time Complexity: O(mn)
  • Space Complexity: O(1)

3402. Minimum Operations to Make Columns Strictly Increasing LeetCode Solution in C++

class Solution {
 public:
  int minimumOperations(vector<vector<int>>& grid) {
    int ans = 0;

    for (int j = 0; j < grid[0].size(); ++j)
      for (int i = 1; i < grid.size(); ++i)
        if (grid[i][j] <= grid[i - 1][j]) {
          ans += grid[i - 1][j] - grid[i][j] + 1;
          grid[i][j] = grid[i - 1][j] + 1;
        }

    return ans;
  }
};
/* code provided by PROGIEZ */

3402. Minimum Operations to Make Columns Strictly Increasing LeetCode Solution in Java

class Solution {
  public int minimumOperations(int[][] grid) {
    int ans = 0;

    for (int j = 0; j < grid[0].length; ++j)
      for (int i = 1; i < grid.length; ++i)
        if (grid[i][j] <= grid[i - 1][j]) {
          ans += grid[i - 1][j] - grid[i][j] + 1;
          grid[i][j] = grid[i - 1][j] + 1;
        }

    return ans;
  }
}
// code provided by PROGIEZ

3402. Minimum Operations to Make Columns Strictly Increasing LeetCode Solution in Python

class Solution:
  def minimumOperations(self, grid: list[list[int]]) -> int:
    ans = 0

    for j in range(len(grid[0])):
      for i in range(1, len(grid)):
        if grid[i][j] <= grid[i - 1][j]:
          ans += grid[i - 1][j] - grid[i][j] + 1
          grid[i][j] = grid[i - 1][j] + 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.