leetcode每日一題-598:范圍求和(二)
鏈接
范圍求和 II
題目

分析
每次添加都是從(0,0)加到(a,b),(0,0)一定是統計次數最多的,所以我們找到所有的等于(0,0)的值即可,那么矩陣中的哪些值等于(0,0)中的值呢,我們這樣來考慮,找到所有a中的最小值x,找到所有b中的最小值y,那么因為x,y分別是a,b的最小值,所以他們和(0,0)一樣一直被+1,所以答案就顯而易見了.
代碼
C++
class Solution {
public:
int maxCount(int m, int n, vector<vector<int>>& ops) {
int x = m, y = n;
for(auto& ve : ops)
{
x = min(x, ve[0]);
y = min(y, ve[1]);
}
return x * y;
}
};
Java
class Solution {
public int maxCount(int m, int n, int[][] ops) {
int mina = m, minb = n;
for (int[] op : ops) {
mina = Math.min(mina, op[0]);
minb = Math.min(minb, op[1]);
}
return mina * minb;
}
}
作者:LeetCode-Solution
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/352144.html
標籤:其他
上一篇:LeetCode:598. 范圍求和 II————簡單
下一篇:kafka 安裝和啟動
