algorithm notes: leetcode#598 range addition ii

Problem


Solution


Initial thoughts

Python implementation

1
2
3
4
5
6
7
8
9
10
11
12
class (object):
def maxCount(self, m, n, ops):
"""
:type m: int
:type n: int
:type ops: List[List[int]]
:rtype: int
"""
for a, b in ops:
m = min(m, a)
n = min(n, b)
return m * n

Java implementation

1
2
3
4
5
6
7
8
9
class {
public int maxCount(int m, int n, int[][] ops) {
for(int[] op : ops){
m = m < op[0] ? m : op[0];
n = n < op[1] ? n : op[1];
}
return m*n;
}
}

Time complexity

O(n).

Space complexity

O(1).


598. Range Addition II
(中文版) 算法笔记: 力扣#598 范围求和 II