LeetCode Range Addition II
原题链接在这里: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:
- 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.
题解:
找出最小的更新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;
}
}
LeetCode Range Addition II的更多相关文章
- [LeetCode] Range Addition II 范围相加之二
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- [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 范围相加之二
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- 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.提供一个二 ...
随机推荐
- VSCode eslint校验 tab改为2个空格
修改:.eslintrc.json
- PHP连接到mysql的方法--mysqli和PDO
php连接到mysql数据库,经典的方式就是使用mysql_connect(),具体代码如下: mysql_connect($db_host, $db_user, $db_pass) or die(m ...
- R的t-test检验
1.t-test的功能:单因素二水平的假设检验. H0:与我们想过要的结果相反的假设,比如我们想要的是两组数据的差异性,那么这个假设是:两组数据没有差异性. H1或Ha:备择假设,与H0假设相反. 2 ...
- python:格式化输出 str.format()
官网说明:https://docs.python.org/2/library/string.html#formatstrings python的格式输出有两种方法: 1.“ %s”.(variant) ...
- docker link
什么是docker的link机制 同一个宿主机上的多个docker容器之间如果想进行通信,可以通过使用容器的ip地址来通信,也可以通过宿主机的ip加上容器暴露出的端口号来通信,前者会导致ip地址的硬编 ...
- MyEclipse Could not create the view: An unexpected exception was thrown解决方案
问题:电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected excep ...
- 把已安装的wampserver移动到不同目录使用应注意的问题
很多时候需要把已安装的wampserver移动到不同目录使用,此时应注意几个问题: 1.修改D:\wamp64\bin\apache\apache2.4.9\conf目录下的httpd.conf文件( ...
- 【BZOJ1854】游戏[SCOI2009](神奇贪心+并查集)
这道题和今年GDKOI的Day2T2很像(然而gdkoi的题用网络流可以A,这道题只能拿30). 网址:http://www.lydsy.com/JudgeOnline/problem.php?id= ...
- PostgreSQL的日志文件介绍
PostgreSQL的日志文件 pg_log:数据库活动日志(也就是数据库的操作日志): pg_xlog:事务日志: pg_clog:事务状态日志(pg_clog是pg_xlog的辅助日志). 现在主 ...
- Codeforces 895C Square Subsets:状压dp【组合数结论】
题目链接:http://codeforces.com/problemset/problem/895/C 题意: 给你n个数a[i].(n <= 10^5, 1 <= a[i] <= ...