[
]
Leetcode 0598 Range Addition II
Problem statement
https://leetcode.com/problems/range-addition-ii/
Solution
We can note, that [0, 0]
will always be increased by 1
for every range we have. What cells also will be increased by 1
for every range? It is all coordinates, where x <= min(all x cooridnates)
and y <= min(all y coordinates)
.
So we need to find minimum of all x
coordinates and multiply it by minimum of all y
coordinates. If we have empty update list, we need to return m * n
(because all elements still equal to 0
) so we can add it to our ops
.
Complexity
Time complexity is O(k)
, where k
is number of updates, space complexity is O(1)
.
Code
class Solution:
def maxCount(self, m, n, ops):
return min(i for i,_ in ops+[[m,n]]) * min(j for _,j in ops+[[m,n]])