• + 0 comments

    Here, an easiest way to understand in ruby, at least for me.

    def arrayManipulation(n, queries)
        arr = Array.new(n + 1, 0)
        queries.each do |q|
            arr[q.first - 1] += q.last
            arr[q[1]] -= q.last
        end
        tmp = 0
        max = 0
        arr.each do |n|
            tmp += n
            max = tmp if tmp > max
        end
        max
    end