题目:

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:

  1. The range of m and n is [1,40000].
  2. The range of a is [1,m], and the range of b is [1,n].
  3. The range of operations size won't exceed 10,000.

代码:

没太看懂题,稍后再做。

参考:http://www.cnblogs.com/grandyang/p/6974232.html

【这道题看起来像是之前那道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[0].empty()) return m * n;
priority_queue<int> r, c;
for (auto op : ops) {
r.push(-op[0]);
c.push(-op[1]);
}
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[0]);
n = min(n, op[1]);
}
return m * n;
}
};

LeetCode: 598 Range Addition II(easy)的更多相关文章

  1. [LeetCode] 598. Range Addition II 范围相加之二

    Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...

  2. LeetCode 598. Range Addition II (范围加法之二)

    Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...

  3. 【leetcode_easy】598. Range Addition II

    problem 598. Range Addition II 题意: 第一感觉就是最小的行和列的乘积即是最后结果. class Solution { public: int maxCount(int ...

  4. 598. Range Addition II 矩阵的范围叠加

    [抄题]: Given an m * n matrix M initialized with all 0's and several update operations. Operations are ...

  5. 【LeetCode】598. Range Addition II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. [LeetCode&Python] Problem 598. Range Addition II

    Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...

  7. 【leetcode】598. Range Addition II

    You are given an m x n matrix M initialized with all 0's and an array of operations ops, where ops[i ...

  8. 598. Range Addition II

    Given an m * n matrixMinitialized with all0's and several update operations. Operations are represen ...

  9. [LeetCode] 598. Range Addition II_Easy tag: Math

    做个基本思路可以用 brute force, 但时间复杂度较高. 因为起始值都为0, 所以肯定是左上角的重合的最小的长方形就是结果, 所以我们求x, y 的最小值, 最后返回x*y. Code    ...

随机推荐

  1. opencv常用类总结

    1 Rect_ (const Point_< _Tp > &pt1, const Point_< _Tp > &pt2),Rect的这种两个点的构造函数的两个点 ...

  2. Error524 源站处理超时 Error 524: A timeout occurred

    https://su.baidu.com/helps/index.html#/4/5a61e4b5b34f697f13234a5b Error524 源站处理超时 更新时间:2018-01-19 20 ...

  3. 解决win7打印机共享出现“无法保存打印机设置(错误0x000006d9)的问题

    最新解决win7打印机共享出现“无法保存打印机设置(错误0x000006d9)的问题,由系统下载吧率先分享: 有些用户在使用Windows7系统过程中,碰到到win7打印机共享出现“无法保存打印机设置 ...

  4. 我的Android进阶之旅------>Android检测wifi连接状态

    今天要实现监听系统Wifi连接状态,下面代码简化后提取出来的,以备后用. step1. 编写BroadcastReceiver import android.content.BroadcastRece ...

  5. JDK动态proxy原理解析

    转: 之前虽然会用JDK的动态代理,但是有些问题却一直没有搞明白.比如说:InvocationHandler的invoke方法是由谁来调用的,代理对象是怎么生成的,直到前几个星期才把这些问题全部搞明白 ...

  6. Ruby操作数据库基本步骤

    1.rails g model university name:string 2.model has_many :colleges belongs_to :university has_one :us ...

  7. mac 在 finder 当前 路径下 打开 terminal 的办法

    1. 在:系统偏好设置 -> 键盘 -> 服务 或者 finder -> 服务偏好设置, 如下: 建议配合快捷键使用,本人使用的快捷键: 在 terminal 新建标签 contro ...

  8. BFC和haslayout(IE6-7)(待总结。。。)

    支持BFC的浏览器(IE8+,firefox,chrome,safari) Block Formatting Context(块格式化上下文)是W3C CSS2.1规范中的一个慨念,在CSS3中被修改 ...

  9. LNMP安装(二)

    PHP安装 1.yum安装一些依赖库 yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel ...

  10. inode、软连接、硬链接

    一.inode是什么? 理解inode,要从文件储存说起.文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB).操作系统读取 ...