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:

  1. The range of m and n is [1,40000].
  2. The range of a is [1,m], and the range of b is [1,n].
  3. 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

关键词:matrix, 2d array

关键点:把题目转换成图形帮助理解

 1 public class Solution
 2 {
 3     public int maxCount(int m, int n, int[][] ops)
 4     {
 5         if(ops == null || ops.length == 0 )
 6             return m*n;
 7
 8         int x = Integer.MAX_VALUE;
 9         int y = x;
10
11
12
13         for(int i=0; i<ops.length; i++)
14         {
15             if(ops[i][0] < x)
16                 x = ops[i][0];
17             if(ops[i][1] < y)
18                 y = ops[i][1];
19
20         }
21
22         return x*y;
23     }
24 }

参考资料:

N / A

时间: 2024-12-24 00:52:26

LeetCode 598. Range Addition II (区域范围内加法)的相关文章

[LeetCode] 598. Range Addition II

https://leetcode.com/problems/range-addition-ii 思路1: brute force - O(m * n * k), k = number of ops思路2: 从 ops 中找到被操作次数最多的 row 和 column,这可以通过遍历 ops 数组,分别找到 row 和 column 的最小值来完成参考: https://discuss.leetcode.com/topic/90547/java-solution-find-min public c

[LeetCode&amp;Python] Problem 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

598. Range Addition II

Problem statement: 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

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 

LeetCode:Contains Duplicate II - 判断数组内是否有重复元素2

1.题目名称 Contains Duplicate II(判断数组内是否有重复元素2) 2.题目地址 https://leetcode.com/problems/contains-duplicate-ii/ 3.题目内容 英文:Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nu

[LeetCode] 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

LeetCode 370. Range Addition (范围加法)$

Assume you have an array of length n initialized with all 0's and are given k update operations. Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each element of subarray A[startIndex ... endIndex] (startIndex

LeetCode Range Addition

原题链接在这里:https://leetcode.com/problems/range-addition/description/ 题目: Assume you have an array of length n initialized with all 0's and are given k update operations. Each operation is represented as a triplet: [startIndex, endIndex, inc] which incre

LeetCode:Range Sum Query - Immutable - 数组指定区间内的元素和

1.题目名称 Range Sum Query(数组指定区间内的元素和) 2.题目地址 https://leetcode.com/problems/range-sum-query-immutable/ 3.题目内容 英文:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 中文:给定一个数组nums,求出索引i和j之间元素的和,i一定是小于或等于j