【LeetCode】598. Range Addition II 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: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.
题目大意
对于一个初始全部为零的二维数组,经过一串操作后,找出数组中最大的数字出现的次数
。操作是指,把二维数组中小于(m,n)左上角的元素都 + 1.
解题方法
首先要明白,要求的结果是最终二维数组中最大值出现的次数
。那么,被重复加的最多的数组元素一定最大,其出现次数就是答案。
每次操作都是对左上角部分区域进行操作的,并且,a,b的范围都是大于1的,即每次都会有元素改变。那么,最终二维数组中最大值出现的次数一定是操作中最小范围中的那批元素构成的矩形区域面积。
如果没有进行操作,那么,返回的应该是全部0的个数。
class Solution(object):
def maxCount(self, m, n, ops):
"""
:type m: int
:type n: int
:type ops: List[List[int]]
:rtype: int
"""
if not ops:
return m * n
return min([op[0] for op in ops]) * min([op[1] for op in ops])
日期
2018 年 2 月 28 日
2018 年 11 月 15 日 —— 时间太快,不忍直视
【LeetCode】598. Range Addition II 解题报告(Python)的更多相关文章
- [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 598. Range Addition II (范围加法之二)
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- 【leetcode_easy】598. Range Addition II
problem 598. Range Addition II 题意: 第一感觉就是最小的行和列的乘积即是最后结果. class Solution { public: int maxCount(int ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 598. Range Addition II 矩阵的范围叠加
[抄题]: Given an m * n matrix M initialized with all 0's and several update operations. Operations are ...
- [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】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
随机推荐
- requests+bs4爬取豌豆荚排行榜及下载排行榜app
爬取排行榜应用信息 爬取豌豆荚排行榜app信息 - app_detail_url - 应用详情页url - app_image_url - 应用图片url - app_name - 应用名称 - ap ...
- 非标准的xml解析器的C++实现:一、思考基本数据结构的设计
前言: 我在C++项目中使用xml作为本地简易数据管理,到目前为止有5年时间了,从最初的全文搜索标签首尾,直到目前项目中实际运用的类库细致到已经基本符合w3c标准,我一共写过3次解析器,我自己并没有多 ...
- 非寻常方式学习ApacheTomcat架构及10.0.12源码编译
概述 开启博客分享已近三个月,感谢所有花时间精力和小编一路学习和成长的伙伴们,有你们的支持,我们继续再接再厉 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 Tomcat官 ...
- 16. Linux find查找文件及文件夹命令
find的主要用来查找文件,查找文件的用法我们比较熟悉,也可用它来查找文件夹,用法跟查找文件类似,只要在最后面指明查找的文件类型 -type d,如果不指定type类型,会将包含查找内容的文件和文件夹 ...
- Learning Spark中文版--第六章--Spark高级编程(1)
Introduction(介绍) 本章介绍了之前章节没有涵盖的高级Spark编程特性.我们介绍两种类型的共享变量:用来聚合信息的累加器和能有效分配较大值的广播变量.基于对RDD现有的transform ...
- deque、queue和stack深度探索(上)
deque是可双端扩展的双端队列,蓝色部分就是它的迭代器类,拥有四个指针,第一个cur用来指向当前元素,first指向当前buffer头部,last指向当前buffer尾部,node指向map自己当前 ...
- binlog2sql 解析日志失败 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 0: invalid start byte
python35 ./binlog2sql.py -h... -P... -u... -p... -B --start-file="mysql-bin.091940" --star ...
- HelloWorldDynamic
package mbeanTest; import java.lang.reflect.Method; import javax.management.Attribute; import javax. ...
- Oracle删除重复数据记录
删除重复记录,利用ROWID 和MIN(或MAX)函数, ROWID在整个数据库中是唯一的,由Oracle自己产生和维护,并唯一标识一行(无论该表中是否有主键和唯一性约束),ROWID确定了每条记录在 ...
- 【Java基础】方法调用机制——MethodHandle
MethodHandle是Java7引入的一种机制,主要是为了JVM支持动态语言. 一个MethodHandle调用示例 共有方法调用 首先,演示一下最基本的MethodHandle使用. 第一步:创 ...