7/17/2021

[LeetCode] 370. Range Addition

 Problem : https://leetcode.com/problems/range-addition/

Use scan line approach to increase value when update section starts and decrease value when update section ends.

Time complexity = O( M + N ) ,   M = len(updates), N = length.


class Solution:
    def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]:
        
        memo = defaultdict(int)
        
        for a, b, d in updates:
            memo[a] += d
            memo[b+1] -= d
            
        value = 0
        result = []
        for i in range(length):
            value += memo[i]
            result.append(value)
        
        return result

No comments:

Post a Comment