首页 > 代码库 > [LeetCode] 598. Range Addition II

[LeetCode] 598. Range Addition II

https://leetcode.com/problems/range-addition-ii

思路1: brute force - O(m * n * k), k = number of ops
思路2: 从 ops 中找到被操作次数最多的 row 和 column,这可以通过遍历 ops 数组,分别找到 row 和 column 的最小值来完成
参考: https://discuss.leetcode.com/topic/90547/java-solution-find-min

 

public class Solution {
    public int maxCount(int m, int n, int[][] ops) {
        if (ops == null || ops.length == 0) {
            return m * n;
        }
        int row = ops[0][0];
        int column = ops[0][1];
        for (int i = 1; i < ops.length; i++) {
            row = Math.min(row, ops[i][0]);
            column = Math.min(column, ops[i][1]);
        }
        return row * column;
    }
}

 

[LeetCode] 598. Range Addition II