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 ...
随机推荐
- 在Ubuntu中部署并测试Fabric 1.0 Beta
[更新:1.0Beta已经是过去式了,现在出了1.0.0的正式版,请大家参照 http://www.cnblogs.com/studyzy/p/7437157.html 安装Fabric 1.0.0 ...
- [04] Cookie概念和基本使用
1.Cookie是什么 Cookie,中文名称为"小型文本文件"或"小甜饼",指某些网站为了辨别用户身份而储存在用户本地终端上的数据(通常经过加密). 很多网站 ...
- linux加载与使用ko驱动
linux驱动和有两种形式: 1:编译到内核 2:编译为ko模块 这里记录下ko模块使用方法. 首先cd到/var/lib/(内核版本)/drivers/ 在这里面找到要装载的模块ko文件 modpr ...
- [python学习笔记] python程序打包成exe文件
安装 pyinstaller pip3 install pyinstaller 命令 pyinstaller -F -w -i ../ui/icon.ico --clean ../Login.py 参 ...
- Django查询数据库性能优化
现在有一张记录用户信息的UserInfo数据表,表中记录了10个用户的姓名,呢称,年龄,工作等信息. models文件 from django.db import models class Job(m ...
- 最详细的cookie和浏览隐私之间的关系
本文所说的"cookie",指的是浏览器相关的 cookie(也叫"HTTP cookie"). 浏览器 cookie 的主要功能是:帮助网站保存一些小片段的信 ...
- ThinkPHP中foreach和volist的区别
1.foreach标签foreach标签用于循环输出:foreach(name,item,key)name(必须):要输出的数据模板变量item(必须):循环单原变量key(可选):循环的key变量, ...
- DLL生成与使用的全过程
由dll导出的lib文件: 包含了每一个dll导出函数的符号名和可选择的标识号以及dll文件名,不含有实际的代码(这里的lib文件和静态库是不一样的),其中的导出导入函数都 是跳转指令,直接跳转到DL ...
- Json操作问题总结
大家都知道,Json是一种轻量级的数据交换格式,对JS处理数据来说是很理想滴! 熟练写过xxx.json文件和操作的小伙伴来说,我说的问题都不是什么大问题啦,可以忽略本宝宝的文章,更希望各位大佬指点一 ...
- Ionic3学习笔记(三)禁止横屏
本文为原创文章,转载请标明出处 目录 安装 使用 参数 1. 安装 命令行输入 ionic cordova plugin add cordova-plugin-screen-orientation n ...