原题链接在这里:https://leetcode.com/problems/range-addition-ii/description/

题目:

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.

题解:

找出最小的更新row, 和最小的更新column, 返回乘机. 记得这两个可能比对应的m, n大, 所以初始值时m,n.

Time Complexity: O(ops.length). Space: O(1).

AC Java:

 class Solution {
public int maxCount(int m, int n, int[][] ops) {
if(ops == null || ops.length == 0){
return m*n;
} int minRow = m;
int minColumn = n;
for(int [] op : ops){
minRow = Math.min(minRow, op[0]);
minColumn = Math.min(minColumn, op[1]);
}
return minRow*minColumn;
}
}

类似Range Addition.

LeetCode Range Addition II的更多相关文章

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

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

  2. [LeetCode] Range Addition 范围相加

    Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...

  3. 【leetcode_easy】598. Range Addition II

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

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

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

  5. LeetCode: 598 Range Addition II(easy)

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

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

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

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

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

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

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

  9. LeetCode算法题-Range Addition II(Java实现)

    这是悦乐书的第271次更新,第285篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第138题(顺位题号是598).给定一个m行n列的新二维数组M,其初始值为0.提供一个二 ...

随机推荐

  1. HNOI2019梦游记

    \(Day_0\) 十点半开始睡觉,开始了八个小时的不眠之夜,整晚都没睡着,这状态明天肯定挂了 \(Day_1\) 开局一条鱼,计算几何只会\(20\) 还是\(T2\)的\(20\)纯暴力好打,\( ...

  2. 20145240 《Java程序设计》第十周学习总结

    20145240 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 程序员所作的事情就是把数据发送到指定的位置 ...

  3. Kubernetes busybox nslookup问题

    使用最新版本的busybox会出现nslookup提示无法解析的问题: Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find k ...

  4. 输入框去除默认的文字,jquery方法

    需求:所有的输入框获取焦点时,去掉默认的提示文字,失去焦点时如果输入框为空,恢复默认的提示文字. 解决方案:jquery方法,以下有三种,按照利弊,我建议最后一种. 先看html代码: <inp ...

  5. Datax官方笔记总结

    # DataX DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL.SQL Server.Oracle.PostgreSQL.HDFS.Hive.HBase.OTS. ...

  6. R语言笔记006——分组获取描述性统计量

    方法一:使用aggregate()分组获取描述性统计量 aggregate(mtcars[vars],by=list(am=mtcars$am),mean) aggregate(mtcars[vars ...

  7. hadoop mapreduce实现数据去重

    实现原理分析: map函数数将输入的文本按照行读取,   并将Key--每一行的内容   输出    value--空. reduce  会自动统计所有的key,我们让reduce输出key-> ...

  8. Gerrit使用感受

    CodeReivew好工具,可以随业务需求灵活配置权限等.

  9. Can't connect to MySQL server on 'localhost' (10061)的解决办法!

    Can't connect to MySQL server on 'localhost' (10061)的解决办法! http://blog.sina.com.cn/s/blog_52ebca1f01 ...

  10. 解决Chrome Safari Opera环境下 动态创建iframe onload事件同步执行

    我们先看下面的代码: setTimeout(function(){ alert(count); },2000); var count = []; document.body.appendChild(c ...