[LeetCode] Range Addition II 范围相加之二
Given an m * n matrix M initialized with all 0's and several update operations.
Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should be added by one for all 0 <= i < a and 0 <= j < b.
You need to count and return the number of maximum integers in the matrix after performing all the operations.
Example 1:
Input:
m = 3, n = 3
operations = [[2,2],[3,3]]
Output: 4
Explanation:
Initially, M =
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]] After performing [2,2], M =
[[1, 1, 0],
[1, 1, 0],
[0, 0, 0]] After performing [3,3], M =
[[2, 2, 1],
[2, 2, 1],
[1, 1, 1]] So the maximum integer in M is 2, and there are four of it in M. So return 4.
Note:
- The range of m and n is [1,40000].
- The range of a is [1,m], and the range of b is [1,n].
- The range of operations size won't exceed 10,000.
这道题看起来像是之前那道 Range Addition 的拓展,但是感觉实际上更简单一些。每次在 ops 中给定我们一个横纵坐标,将这个子矩形范围内的数字全部自增1,让我们求最大数字的个数。原数组初始化均为0,那么如果 ops 为空,没有任何操作,那么直接返回 m*n 即可,我们可以用一个优先队列来保存最大数字矩阵的横纵坐标,我们可以通过举些例子发现,只有最小数字组成的边界中的数字才会被每次更新,所以我们想让最小的数字到队首,更优先队列的排序机制是大的数字在队首,所以我们对其取相反数,这样我们最后取出两个队列的队首数字相乘即为结果,参见代码如下:
解法一:
class Solution {
public:
int maxCount(int m, int n, vector<vector<int>>& ops) {
if (ops.empty() || ops[].empty()) return m * n;
priority_queue<int> r, c;
for (auto op : ops) {
r.push(-op[]);
c.push(-op[]);
}
return r.top() * c.top();
}
};
我们可以对空间进行优化,不使用优先队列,而是每次用 ops 中的值来更新m和n,取其中较小值,这样遍历完成后,m和n就是最大数矩阵的边界了,参见代码如下:
解法二:
class Solution {
public:
int maxCount(int m, int n, vector<vector<int>>& ops) {
for (auto op : ops) {
m = min(m, op[]);
n = min(n, op[]);
}
return m * n;
}
};
Github 同步地址:
类似题目:
参考资料:
https://leetcode.com/problems/range-addition-ii/
https://leetcode.com/problems/range-addition-ii/discuss/103595/Java-Solution-find-Min
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Range Addition II 范围相加之二的更多相关文章
- [LeetCode] 598. Range Addition II 范围相加之二
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- LeetCode Range Addition II
原题链接在这里:https://leetcode.com/problems/range-addition-ii/description/ 题目: Given an m * n matrix M ini ...
- [LeetCode] Range Addition 范围相加
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
- 【leetcode_easy】598. Range Addition II
problem 598. Range Addition II 题意: 第一感觉就是最小的行和列的乘积即是最后结果. class Solution { public: int maxCount(int ...
- LeetCode: 598 Range Addition II(easy)
题目: Given an m * n matrix M initialized with all 0's and several update operations. Operations are r ...
- 598. Range Addition II 矩阵的范围叠加
[抄题]: Given an m * n matrix M initialized with all 0's and several update operations. Operations are ...
- LeetCode 598. Range Addition II (范围加法之二)
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- 【LeetCode】598. Range Addition II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode算法题-Range Addition II(Java实现)
这是悦乐书的第271次更新,第285篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第138题(顺位题号是598).给定一个m行n列的新二维数组M,其初始值为0.提供一个二 ...
随机推荐
- KVM之七:KVM克隆
1.在克隆虚拟机之前,必须先暂停或者停掉kvm 虚拟机.以虚拟机 snale 为例,先暂停虚拟机,如下 [root@kvm ~ ::]#virsh list Id 名称 状态 ------------ ...
- Ubuntu16.0.4下搭建pycharm 2018.3.22
一.首先安装Java jdk Java JDK有两个版本,一个开源版本Openjdk,还有一个Oracle官方版本jdk.下面记录在Ubuntu 16.04上安装Java JDK的步骤. 安装open ...
- 实现win的on程序数据更新
枚举是一组描述性的名称定义一组有限的值,不包含方法对可能的值进行约束枚举是一组指定的常数,对可能的值进行约束枚举使用时直观方便.更易于维护 pictureBox控件属性名称 说明image ...
- ResultSet的getInt()和getString()方法详解
数据库tt的examstudent数据表如下: 在MySQL中执行查询语句如下: ResultSet rs = null; String sql="SELECT flow_id,Typ ...
- python内置函数 divmod()
先来看一下builtins.py中的代码: def divmod(x, y): # known case of builtins.divmod """ Return th ...
- php项目中常用的log日志记录方法
function log_result($str) { if (LOG_WRITEOUT == 1) { $fp = fopen ( "log.txt", "a+&quo ...
- C语言第一次博客作业—输入输出
一.PTA实验作业 题目1:7-3 温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1 ...
- 201621123062《java程序设计》第二周学习总结
1.本周学习总结 本周学习重点: 1.java的基本数据类型(类似于C,特有boolean),java的引用类型(类似指针),其他常用类. 2.字符串类型String(比c简单),String的不变性 ...
- 第二次作业:APP案例分析
App案例分析 产品:三国杀-页游手游双通 选择理由 当今社会手机已经渐渐取代了电脑在人们日常生活的需求,既然要选择APP进行案例分析,首推的估计就是手机APP了.三国杀是陪伴我高中时代的主要娱乐方式 ...
- asp.net web api 控制器
1控制器操作的参数 控制器操作的参数可以是内置类型也可以是自定义类型,无参也是允许的. 2控制器操作返回值 类型 说明 void 操作返回值为void时,Web API返回空HTTP响应,其状态码为2 ...