LeetCode 598. 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.
题目标签:Math
这道题目给了我们一个 m*n 的matrix, 起初都是0, 根据operation给其中一部分区域加1。最后要return 最大值integer的个数。
我们可以从另一个角度出发,把这个题目转化成图形来理解,最大的值的区域就是所有operation的交集。如何找到这个区域呢,我们需要记录一个min x 和min y 来求出交集的区域 = x*y, 相当于在求面积。
举两个例子来看一下:
Example 1:
maxCount(3,3,[ [1,2], [2,1] ])
0 0 0 1 1 0 2 1 0
0 0 0 [1,2] -> 0 0 0 [2,1] -> 1 0 0 return 1;
0 0 0 0 0 0 0 0 0
最小的 x = 1, 最小的 y = 1, 所以最小的交集是 0,0 这个坐标, 它的区域 = 1 * 1。
Example 2:
maxCount(3,3,[ [1,3], [2,2] ])
0 0 0 1 1 1 2 2 1
0 0 0 [1,3] -> 0 0 0 [2,2] -> 1 1 0 return 2;
0 0 0 0 0 0 0 0 0
最小的 x = 1, 最小的 y = 2, 所以最小的交集是 0,0 和 0,1 这两个坐标, 它的区域 = 1 * 2。
Java Solution:
Runtime beats 77.83%
完成日期:06/17/2017
关键词:math: matrix
关键点:找到重叠的区域
class Solution
{
public int maxCount(int m, int n, int[][] ops)
{
int minRow = m;
int minCol = n; for(int[] op : ops)
{
minRow = Math.min(minRow, op[0]);
minCol = Math.min(minCol, op[1]);
} return minRow * minCol;
}
}
参考资料:n/a
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 598. 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: 598 Range Addition II(easy)
题目: Given an m * n matrix M initialized with all 0's and several update operations. Operations are r ...
- 【leetcode_easy】598. Range Addition II
problem 598. Range Addition II 题意: 第一感觉就是最小的行和列的乘积即是最后结果. class Solution { public: int maxCount(int ...
- [LeetCode] Range Addition II 范围相加之二
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- 【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 ...
- LeetCode 370. Range Addition (范围加法)$
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
随机推荐
- java程序与编译
Java 源文件(.java) 使用 Java编译器(javac.exe)编译 生成 java字节码文件(.class) 使用 解释执行器(java.exe) 将字节码文件加载到java虚拟机(j ...
- 泛型在Web中的作用
当我们写网页的时候,常常会有多个DAO,我们要写每次都要写好几个DAO,这样会有点麻烦. 那么我们想要的效果是什么呢??只写一个抽象DAO,别的DAO只要继承该抽象DAO,就有对应的方法了. 要实现这 ...
- bookStore项目总结
感想 该项目是目前为止,我写过代码量最多的项目了-..虽然清楚是没有含金量的[跟着视频来写的],但感觉自己也在进步中-.. 写的过程中,出了不少的问题-..非常多的Servlet,JSP看得眼花-.. ...
- MyeclipseJRE版本设置
1.首先添加JDK版本 Window——Preferences——Java——Install JREs——Add——Stand VM——浏览JDK安装版本完成即可(一定是JDK中JRE的安装目录如:D ...
- StringBuffer和String的相互转换
1:用法: * A:String -- >StringBuffer * a:通过构造方法 * b:通过append()方法 * B:StringBuffer --> String * a: ...
- 【京东详情页】——原生js爬坑之放大镜
一.引言 在商城的详情页中,放大镜的功能是很常见的.这里京东详情页就要做一个仿放大镜的效果,预览如下: 二.实现原理 实际上,放大镜的实现是单纯用几个div,鼠标移入其中一个小图div,触发事件显示另 ...
- VCL组件之TLabel、TStaticText和TLabeledEdit
TLabel.TStaticText.TLabeledEdit类的继承关系如下:
- .NET十年回顾
一. 引子 从我还是编程菜鸟时起,.NET就从来没让我失望过.总是惊喜不断. 当年我第一个项目是做个进销存.用的Winform.当时我是机电工程师.编程只是业余心血来潮而已. .NET的低门槛.V ...
- Elevator poj3539
Elevator Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 1072 Accepted: 287 Case Time ...
- Disharmony Trees 树状数组
Disharmony Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...