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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

行列都取最小值

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

表示每次取出的是数组,op[0]表示该被取出的数组中的第0位,op[1]表示该数组中的第1位

//find min, max
for (int[] op : ops) {
row = Math.min(op[0], row);
col = Math.min(op[1], col);
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

370. Range Addition 就是操作数组吧

[代码风格] :

class Solution {
public int maxCount(int m, int n, int[][] ops) {
//cc
if (ops == null || ops.length == 0) return m * n; //ini:min max
int row = Integer.MAX_VALUE, col = Integer.MAX_VALUE; //find min, max
for (int[] op : ops) {
row = Math.min(op[0], row);
col = Math.min(op[1], col);
} return row * col;
}
}
上一篇:js中对象 类 实例的区别 数据类型 创建对象


下一篇:html_jQuery