598.范围求和II
题目描述
https://leetcode-cn.com/problems/range-addition-ii/
思路:求交集
对于每一次操作,给定(a,b),会给矩阵中对应的位置全部加1,因为a、b都为正整数,则(0,0)都满足上述条件,并且最终位置(0,0)的值就等于操作次数。因此,只需要找到矩阵中所有值与(0,0)相等的位置计数即可。
Python实现
class Solution:
def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
row, col = m, n
for r, c in ops:
row = min(row, r)
col = min(col, c)
return row * col
Java实现
class Solution {
public int maxCount(int m, int n, int[][] ops) {
int row = m, col = n;
for (int[] op : ops) {
row = Math.min(row, op[0]);
col = Math.min(col, op[1]);
}
return row * col;
}
}