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.提供一个二 ...
随机推荐
- iOS 52个技巧学习心得笔记 第一章 熟悉OC
1 .简单了解OC2 .在类的头文件中尽量少引入其他头文件3 .多用字面量语法 少用与之等价的方法 4 .多用类型常量 少用 #define 预处理指令5 .用枚举表示状态,选项,状态码 .简单了解O ...
- $python正则表达式系列(5)——零宽断言
本文主要总结了python正则零宽断言(zero-length-assertion)的一些常用用法. 1. 什么是零宽断言 有时候在使用正则表达式做匹配的时候,我们希望匹配一个字符串,这个字符串的前面 ...
- $ListView的优化机制和滑动时数据错乱的讨论
Refer:http://www.myexception.cn/mobile/1612364.html (一)Android ListView的基本用法 1.创建一个实体类Person,为其添加Get ...
- busybox rmmod error — rmmod: chdir(2.6.25): No such file or directory
busybox rmmod error rmmod: chdir(2.6.25): No such file or directory 1. install your modules in dir / ...
- MACHINE_START-内核板级初始化实现机制(linux3.1.0)
转:https://blog.csdn.net/charliewangg12/article/details/41518549 在驱动开发时,我们都是以一块开发板为基础移植驱动程序.每一块开发板对应一 ...
- mysql多表查询原理
转:https://www.cnblogs.com/Toolo/p/3634563.html MySQL的多表查询(笛卡尔积原理) 先确定数据要用到哪些表. 将多个表先通过笛卡尔积变成一个表. 然 ...
- php数组函数-array_merge()
array_merge()函数把两个或多个数组合并为一个数组. 如果键名有重复,该键的键值为最后一个键名对应的值.如果数组是数字 索引,则键名会以连续方式重新索引. 注:如果仅仅向array_merg ...
- Go 语言defer用法
defer延迟调用: 1.确保调用在函数结束时发生: 2.defer列表为先进后出: 3.通常在Open/Close Lock/Unlock中使用. defer调用顺序示例: package mai ...
- 转-centos7下安装apache服务器httpd的yum方式安装
转自Clement-Xu的csdn博客 http://blog.csdn.net/clementad/article/details/41620631 Apache在Linux系统中,其实叫“ht ...
- Excel下载打不开
1.问题描述:今天遇到个问题,对于定时发送邮件,前两天还正常,今天发现邮件能收到,但打不开,显示如下错误: 预览邮件显示: 点击Excel打开,显示如下: 2.问题解决方案 删除对于服务器上部分空间内 ...