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 ...
随机推荐
- delphi xe 3的EhLib 9.0 Build 9.0.033 Full Source安装
1.打开项目文件 2.全选 3.编译和buil 4.添加路径
- centos7配置dhcp
用su 获取root权限 用yum -y install dhcp命令安装dhcp服务(yum是基于RPM包管 理,自动下载RPM包并且安装) 查看安装后生成的配置文件 rpm -qc dhcp 编辑 ...
- Project Euler:Product-sum numbers (problem 88) C++
A natural number, N, that can be written as the sum and product of a given set of at least two natur ...
- SVM原理以及Tensorflow 实现SVM分类(附代码)
1.1. SVM介绍 1.2. 工作原理 1.2.1. 几何间隔和函数间隔 1.2.2. 最大化间隔 - 1.2.2.0.0.1. \(L( {x}^*)\)对$ {x}^*$求导为0 - 1.2.2 ...
- OSGi-入门篇之服务层(03)
前言 作为OSGi框架中最上面的一层,服务层带给了我们更多的动态性,并且使用了大家或多或少都曾了解过的面向服务编程模型,其好处是显而易见的. 1 什么是服务 简单的说,服务就是“为别人所做的工作”,比 ...
- session get和load方法对比
get测试代码如下: public class Test { public static void main(String[] args) { // TODO Auto-generated metho ...
- Linux 虚拟机安装后的配置和一些命令符笔记
一.安装后的配置 1.设在终端的字体为等宽字体 比如:DejaVu Sans Mono 2.将当前的普通用户加入到sudo用户组adduser en sudochmod +w /etc/sudoers ...
- jquery ocupload一键上传文件应用
直接上栗子 这是官方文档栗子 var myUpload = $(element).upload({ name: 'file', action: '', enctype: 'multipart/form ...
- Opengl4.5 中文手册—F
索引 A B C D E F G H I J K L M N O P Q ...
- Nginx学习——Nginx简单介绍和Linux环境下的安装
一:Nginx的简介 百科百科:Nginx Nginx 是一个俄罗斯的哥们开发的,并将其进行了开源. Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器, ...