{x}
blog image

Range Addition II

You are given an m x n matrix M initialized with all 0's and an array of operations ops, where ops[i] = [ai, bi] means M[x][y] should be incremented by one for all 0 <= x < ai and 0 <= y < bi.

Count and return the number of maximum integers in the matrix after performing all the operations.

 

Example 1:

Input: m = 3, n = 3, ops = [[2,2],[3,3]]
Output: 4
Explanation: The maximum integer in M is 2, and there are four of it in M. So return 4.

Example 2:

Input: m = 3, n = 3, ops = [[2,2],[3,3],[3,3],[3,3],[2,2],[3,3],[3,3],[3,3],[2,2],[3,3],[3,3],[3,3]]
Output: 4

Example 3:

Input: m = 3, n = 3, ops = []
Output: 9

 

Constraints:

  • 1 <= m, n <= 4 * 104
  • 0 <= ops.length <= 104
  • ops[i].length == 2
  • 1 <= ai <= m
  • 1 <= bi <= n

Solution Explanation for Range Addition II

This problem involves an m x n matrix initialized with zeros. We perform a series of operations where each operation ops[i] = [aᵢ, bᵢ] increments all cells M[x][y] such that 0 <= x < aᵢ and 0 <= y < bᵢ. The goal is to find the count of the maximum integer in the matrix after all operations are completed.

Approach: Finding the Overlapping Region

Instead of explicitly simulating the matrix updates, we can observe a crucial pattern:

  • Each operation [aᵢ, bᵢ] essentially creates a rectangular region of updates.
  • The final maximum value will be in the submatrix formed by the intersection of all these rectangular regions.
  • This intersection is determined by the minimum aᵢ (number of rows) and minimum bᵢ (number of columns) across all operations.

Therefore, a significantly more efficient approach is to iterate through the ops array, keeping track of the minimum aᵢ and minimum bᵢ. The product of these minimum values gives the number of cells with the maximum value.

Time and Space Complexity Analysis

Time Complexity: O(k), where k is the length of the ops array. We iterate through the ops array once to find the minimum values of aᵢ and bᵢ.

Space Complexity: O(1). We only use a few constant extra variables to store the minimum values and the final result. The space used is independent of the input size (m, n, and k).

Code Implementation in Multiple Languages

The code implementation across different languages is very similar because the core logic is straightforward: iterate, find minimums, and compute the product.

Python:

class Solution:
    def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
        min_a = m
        min_b = n
        for a, b in ops:
            min_a = min(min_a, a)
            min_b = min(min_b, b)
        return min_a * min_b
 

Java:

class Solution {
    public int maxCount(int m, int n, int[][] ops) {
        int min_a = m;
        int min_b = n;
        for (int[] op : ops) {
            min_a = Math.min(min_a, op[0]);
            min_b = Math.min(min_b, op[1]);
        }
        return min_a * min_b;
    }
}

C++:

class Solution {
public:
    int maxCount(int m, int n, vector<vector<int>>& ops) {
        int min_a = m;
        int min_b = n;
        for (const auto& op : ops) {
            min_a = min(min_a, op[0]);
            min_b = min(min_b, op[1]);
        }
        return min_a * min_b;
    }
};

JavaScript:

var maxCount = function(m, n, ops) {
    let min_a = m;
    let min_b = n;
    for (let i = 0; i < ops.length; i++) {
        min_a = Math.min(min_a, ops[i][0]);
        min_b = Math.min(min_b, ops[i][1]);
    }
    return min_a * min_b;
};

The other languages (Go, TypeScript, Rust) would follow a very similar structure, using their respective min functions and array iteration methods. The core algorithmic idea remains consistent.