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 ...
随机推荐
- 浅谈SQL优化入门:2、等值连接和EXPLAIN(MySQL)
1.等值连接:显性连接和隐性连接 在<MySQL必知必会>中对于等值连接有提到两种方式,第一种是直接在WHERE子句中规定如何关联即可,那么第二种则是使用INNER JOIN关键字.如下例 ...
- C# 反射结构体struct的一个坑
今天代码用到了反射赋值,代码是这样写的: var objtype = obj.GetType(); var Fieldinfo = objtype.GetField("I64"); ...
- JAVA_String、StringBuilder、StringBuffer区别
String.StringBuilder.StringBuffer均为字符串 类 需要注意的一些问题 String StringBuilder StringBuffer 一旦创建,不能对其内容进行更改 ...
- [js高手之路] html5 canvas系列教程 - 掌握画直线图形的常用API
我们接着上文[js高手之路] html5 canvase系列教程 - 认识canvas以及基本使用方法继续. 一.直线的绘制 cxt.moveTo( x1, y1 ): 将画笔移动到x1, y1这个点 ...
- java枚举类(enum) 基础知识讲解
枚举类是在java 5后新增的,可以用于封装常量,并且还可以为常量的使用提供一些方法. 定义枚举类的语法: public enum EnumName{ 成员1(A,B...),成员2(A,B...), ...
- Net知识图谱
对于Web系统开发来说,Net其实也是有好多知识点需要学的,虽然目前JAVA是主流,就业市场比较大,但Net也在积极的拥抱开源,大Net Core 2 出来了,这无疑给Net开发者带来更大的希望,好了 ...
- hdu1760博弈SG
A New Tetris Game Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- NOIP2017SummerTraining0717
个人感受:自己水平是真的差劲,和他们不是一个档次的,第二题,如果不是陈载元暴力过了,我也不会那么早去A了第二题,第一题真的是无语,以前做到过,还想到了每个对应值a[i]-i,但是没想出来,真的是 可惜 ...
- 简易RPC框架-过滤器机制
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- Ubuntu16.04 install mysql5.X
打开终端: Ctrl+Alt+T 安装ubuntu自带的mysql-server: sudo apt-get install mysql-server 输出Y按回车如下图: 默认安装为root用户,所 ...