KatInterview

598. Range Addition II

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to find the number of maximum integers in an m x n matrix after several update operations. The matrix starts with all zeros. Each operation [a, b] increments all elements in the top-left sub-matrix of size a x b (i.e., for all 0 <= x < a and 0 <= y < b).

Solution

Range Addition II in Python

class Solution:
    def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
        # Initialize min_row and min_col to the full matrix dimensions.
        # This correctly handles the edge case where `ops` is empty. If ops is empty,
        # the loop is skipped and m * n is returned, which is correct as all
        # elements are 0 (the max value).
        min_row = m
        min_col = n

        # The maximum value in the matrix will be at any cell (i, j) that is
        # affected by all operations. This means i < a and j < b for all [a, b] in ops.
        # This is equivalent to finding the minimum 'a' and minimum 'b'.
        for op in ops:
            # op[0] is the row boundary, op[1] is the column boundary.
            min_row = min(min_row, op[0])
            min_col = min(min_col, op[1])

        # The number of elements with the maximum value is the area of the
        # top-left rectangle defined by the minimum row and column boundaries.
        return min_row * min_col

Range Addition II in Java

class Solution {
    public int maxCount(int m, int n, int[][] ops) {
        // Initialize minRow and minCol to the full matrix dimensions.
        // This handles the edge case where `ops` is empty. In that case, 
        // all m*n elements are 0 (the max value), so we should return m*n.
        int minRow = m;
        int minCol = n;

        // The cells with the maximum value are those incremented by every operation.
        // This corresponds to the intersection of all operation rectangles.
        // Since each operation rectangle starts at (0,0), we just need to find the
        // smallest row and column boundaries.
        for (int[] op : ops) {
            // op[0] is the row boundary, op[1] is the column boundary.
            minRow = Math.min(minRow, op[0]);
            minCol = Math.min(minCol, op[1]);
        }

        // The number of maximum elements is the area of this intersection rectangle.
        return minRow * minCol;
    }
}

Range Addition II in C++

class Solution {
public:
    int maxCount(int m, int n, vector<vector<int>>& ops) {
        // Initialize min_row and min_col to the full matrix dimensions.
        // This handles the case where `ops` is empty, returning m * n.
        int min_row = m;
        int min_col = n;

        // Each operation [a, b] effectively shrinks the region of maximum values.
        // The final region of maximums is the intersection of all operation rectangles.
        // Since all rectangles start at (0,0), the intersection is a rectangle
        // of size min(all a_i) x min(all b_i).
        for (const auto& op : ops) {
            // op[0] is the row boundary, op[1] is the column boundary.
            min_row = std::min(min_row, op[0]);
            min_col = std::min(min_col, op[1]);
        }

        // The number of maximum elements is the area of the smallest rectangle.
        return min_row * min_col;
    }
};

Range Addition II in TypeScript

function maxCount(m: number, n: number, ops: number[][]): number {
    // Initialize minRow and minCol to the full matrix dimensions.
    // This handles the case where `ops` is empty, returning m * n.
    let minRow = m;
    let minCol = n;

    // The cells with the maximum value are those incremented by every operation.
    // This corresponds to the intersection of all operation rectangles.
    // Since each operation rectangle starts at (0,0), we just need to find the
    // smallest row and column boundaries from all operations.
    for (const op of ops) {
        // op[0] is the row boundary, op[1] is the column boundary.
        minRow = Math.min(minRow, op[0]);
        minCol = Math.min(minCol, op[1]);
    }

    // The number of maximum elements is the area of this intersection rectangle.
    return minRow * minCol;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.