LeetCode 598.Range Addition II
考点 | 难度 |
---|---|
Array | Easy |
题目
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.
思路
discussion里面有一个很简单的方法:先将行(r)和列(c)都设为最大值,在ops
里面找最小的行数和最小的列数替换这两个值。因为operatio总是从长方形的左上角开始数,所以r和c相乘得到结果。
答案
public int maxCount(int m, int n, int[][] ops) {
int r = m;
int c = n;
for(int i=0; i<ops.length; i++){
r = Math.min(r,ops[i][0]);
c = Math.min(c,ops[i][1]);
}
return r*c;
}