598. 范围求和 II

image-20211107100449476

解题思路:

​ 这题可以将思路转变为求operations中所表示的所有矩形中相交最多次数的矩形面积,同时根据题目要求,其每个矩形都是以左上角为起点,这样就转换为了求operations中代表矩形的长、宽的最小值,其相乘即为最终结果,同时长、宽的初始值为m,n

以下为C++代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int maxCount(int m, int n, vector<vector<int>>& ops) {
//这题可以引申为求包围的最小的矩形面积
int w=m,h=n;
int len=ops.size();
for(int i=0;i<len;i++){
w=min(ops[i][0],w);
h=min(ops[i][1],h);
}
return w*h;
}
};