返回介绍

solution / 0500-0599 / 0598.Range Addition II / README_EN

发布于 2024-06-17 01:03:36 字数 2880 浏览 0 评论 0 收藏 0

598. Range Addition II

中文文档

Description

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_.

 

Example 1:

Input: m = 3, n = 3, ops = [[2,2],[3,3]]
Output: 4
Explanation: The maximum integer in M is 2, and there are four of it in M. So return 4.

Example 2:

Input: 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]]
Output: 4

Example 3:

Input: m = 3, n = 3, ops = []
Output: 9

 

Constraints:

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

Solutions

Solution 1

class Solution:
  def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
    for a, b in ops:
      m = min(m, a)
      n = min(n, b)
    return m * n
class Solution {
  public int maxCount(int m, int n, int[][] ops) {
    for (int[] op : ops) {
      m = Math.min(m, op[0]);
      n = Math.min(n, op[1]);
    }
    return m * n;
  }
}
class Solution {
public:
  int maxCount(int m, int n, vector<vector<int>>& ops) {
    for (auto op : ops) {
      m = min(m, op[0]);
      n = min(n, op[1]);
    }
    return m * n;
  }
};
func maxCount(m int, n int, ops [][]int) int {
  for _, op := range ops {
    m = min(m, op[0])
    n = min(n, op[1])
  }
  return m * n
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文