(LeetCode)598. 区间加法 II(数学)

题目:598. 区间加法 II

(LeetCode)598. 区间加法 II(数学)_第1张图片
(LeetCode)598. 区间加法 II(数学)_第2张图片

思路:其实最大值的范围就在ops里行、列出现的最小值。时间复杂度0(n)。

C++版本:

class Solution {
public:
    int maxCount(int m, int n, vector<vector<int>>& ops) {
        int mn_r=m,mn_c=n;
        for(auto op:ops){
            mn_r=min(mn_r,op[0]);
            mn_c=min(mn_c,op[1]);
        }
        return mn_r*mn_c;
    }
};

JAVA版本:

class Solution {
    public int maxCount(int m, int n, int[][] ops) {
        int mn_r=m,mn_c=n;
        for(var op:ops){
            mn_r=Math.min(mn_r,op[0]);
            mn_c=Math.min(mn_c,op[1]);
        }
        return mn_r*mn_c;
    }
}

Go版本:

func maxCount(m int, n int, ops [][]int) int {
    var mn_r,mn_c int = m,n
    for _,op := range ops {
        mn_r=min(mn_r,op[0])
        mn_c=min(mn_c,op[1])
    }
    return mn_r*mn_c
}

你可能感兴趣的:(golang版刷题,LeetCode,java版刷题,leetcode,算法,职场和发展,c++,java,golang)