3468. Find the Number of Copy Arrays
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 46% Topics: Array, Math
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
# greedy
class Solution(object):
def countArrays(self, original, bounds):
"""
:type original: List[int]
:type bounds: List[List[int]]
:rtype: int
"""
left, right = bounds[0]
result = right-left+1
for i in xrange(1, len(original)):
diff = original[i]-original[i-1]
left = max(left+diff, bounds[i][0])
right = min(right+diff, bounds[i][1])
result = min(result, max(right-left+1, 0))
return result
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions