Array Manipulation

  • + 0 comments

    Python 3:

    def arrayManipulation(n, queries):
        l = []
        for a, b, k in queries:
            l.append((a-1, k))
            l.append((b, -k))
        l.sort()
        curr_sum = 0
        best = -1
        curr_index = 0
        for idx, val in l:
            if idx != curr_index:
                best = max(best, curr_sum)
                curr_index = idx
            curr_sum += val
        return best