题目:

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. 九度OJ 1028:继续畅通工程 (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3140 解决:1338 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有 ...

  2. c++对象内存的分配

    1 关于c++的对象 只要是用了class或者struct定义的,都是对象,不管有没有方法.不过,一般情况下,没有方法的对象用struct关键字来定义. 2 不用new关键字定义对象 要看这样的对象在 ...

  3. css多余字符显示省略号

    width:300px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; ;

  4. Javascript模块化编程-初识[1]

    JS模块化编程,已经成为一个迫切的需求.理想情况下,开发者只需要实现核心业务逻辑,其他都可以加载别人已经写好的模块. 但是,JS不是一种模块化编程语言,它不支持类,所以没有严格意义上的模块.为了实现模 ...

  5. Java for LeetCode 125 Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  6. :style动态设置属性

    前段时间做页面时需要动态设置背景图片,每一种框架都会遇见类似的需求,特记录下来,以免不时之需: <!DOCTYPE html> <html> <head> < ...

  7. Struts2 自定义输入校验 第五弹

    Struts2的校验框架有两种:一种是validate方法,另一种是有效的xml文件. Action中自定义方法的输入校验,对于通过action的method属性所指定的自定义方法myExecute, ...

  8. linux应用之jdk环境的安装(centos)

    一.yum安装 1.执行:yum search jdk 已加载插件:fastestmirror, securityLoading mirror speeds from cached hostfile ...

  9. 时间序列数据库——索引用ES、聚合分析时加载数据用什么?docvalues的列存储貌似更优优势一些

    加载 如何利用索引和主存储,是一种两难的选择. 选择不使用索引,只使用主存储:除非查询的字段就是主存储的排序字段,否则就需要顺序扫描整个主存储. 选择使用索引,然后用找到的row id去主存储加载数据 ...

  10. AtCoder Beginner Contest 104

    A - Rated for Me Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement A ...