[leetcode]598. Range Addition II
Analysis
ummm~—— [ummmm~]
Given an m * n matrix M initialized with all 0’s and several update operations.
Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should be added by one for all 0 <= i < a and 0 <= j < b.
You need to count and return the number of maximum integers in the matrix after performing all the operations.
如果按照他题目的意思定义二维数组,然后一步步计算的话,最后提交会显示内存溢出,所以只能另辟蹊径。因为他每次都是从M[0][0] 开始加一,所以只要计算ops二维数组里最小的row和最小的column就行了。
Implement
class Solution {
public:
int maxCount(int m, int n, vector<vector<int>>& ops) {
int minr = m;
int minl = n;
int len = ops.size();
for(int i=0; i<len; i++){
minr = min(minr, ops[i][0]);
minl = min(minl, ops[i][1]);
}
return minr*minl;
}
};