598.区域求和II(javascript)598.RangeAdditionII

原创
小哥 3年前 (2022-11-10) 阅读数 9 #大杂烩

给你一个 m x n 的矩阵 M ,全部在初始化时。 0 以及一系列操作 op ,其中 ops[i] = [ai, bi] 意味着当所有 0 <= x < ai 和 0 <= y < bi 时, M[x][y] 应该加 1。

在 执行所有操作后 ,计算并返回 矩阵中最大整数的数目 。

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.

示例 1:

输入: m = 3, n = 3,ops = [[2,2],[3,3]]
输出: 4
解释: M 中的最大整数为 2, 而且 M 中有4个值为2要素所以返回 4。

示例 2:

输入: m = 3, n = 3, ops = [[2,2],[3,3],[3,3],[3,3],[2,2],[3,3],[3,3],[3,3],[2,2],[3,3],[3,3],[3,3]]
输出: 4

示例 3:

输入: m = 3, n = 3, ops = []
输出: 9

提示:

1 <= m, n <= 4 * 104
0 <= ops.length <= 104
ops[i].length == 2
1 <= ai <= m
1 <= bi <= n

思考解决问题,寻找ops数组里面ops[i][0]最小值,ops[i][1]的最小值
初始化x为m,y为n ,x*y获得的最大值

/**
 * @param {number} m
 * @param {number} n
 * @param {number[][]} ops
 * @return {number}
 */
var maxCount = function (m, n, ops) {
    let minX = m, minY = n;
    for (let i = 0; i < ops.length; i++) {
        minX = Math.min(ops[i][0], minX)
        minY = Math.min(ops[i][1], minY)
    }
    return minX * minY
};

var maxCount = function(m, n, ops) {
    let minX = m , minY = n;
    for (const op of ops) {
        minX = Math.min(minX, op[0]);
        minY = Math.min(minY, op[1]);
    }
    return minX * minY;
};

leetcode: https://leetcode.cn/problems/range-addition-ii/

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除