Sort by

recency

|

2504 Discussions

|

  • + 0 comments

    Mi solución en Python 3:

    def arrayManipulation(n, queries):
        # Write your code here
        arr = [0] * (n + 1) 
        for a, b, k in queries: 
            arr[a-1] += k 
            arr[b] -= k 
            max_val = 0 
            current = 0 
            for val in arr: 
                current += val 
                if current > max_val: 
                    max_val = current 
        return max_val
    
  • + 0 comments

    First solution: Populate the main array with the final values and then calculate the max:

    def arrayManipulation(n, queries) 
    
        ary = [0] * (n+1)
        diff = [0] * (n+1)    
        queries.each do |a, b, k|
            diff[a] += k
            diff[b+1] -= k if b < n
        end    
        for i in (1..n)
            ary[i] = ary[i-1] + diff[i]
        end
        ary.max    
    		
    end
    

    Second solution: While computing the running sum, we track the maximum value encountered.That maximum is the final answer. We save on some space by using this solution.

    `

    def arrayManipulation(n, queries)
        diff = [0] * (n+1)    
        queries.each do |a, b, k|
            diff[a] += k
            diff[b+1] -= k if b < n
        end        
        running_sum = 0
        max = diff[1]
        for i in (1..n)
            running_sum = running_sum + diff[i] 
            max = [max, running_sum].max
        end
        max
        
    end
    
  • + 0 comments

    We use a helper array, called diff, to efficiently track the range updates.

    For each operation (a, b, k):

    • Add k to diff[a] to mark where the increment starts
    • Subtract k from diff[b + 1] to mark where the increment ends

    So we perform:

    diff[a] += k
    diff[b + 1] -= k
    

    I'll post the solution in the following posts:

    After processing all operations, we traverse the diff array from left to right while maintaining a running sum. The final value at each index is obtained by adding the current value in diff to the running sum of all previous values.

  • + 0 comments

    this task has so many flows in its wording that I even dont know where to start. The only difuclty of the task is simply guessing what the aim is. I suspect you can pass it in secons with one assumtion: you have seen it before and you know already what it really means. This would never be an acceptable official exam type question or would be discared after the first trial.

  • + 0 comments

    Here's my adopted solution for Golang, credits to mwwhite

    func arrayManipulation(n int32, queries [][]int32) int64 {
        var maxValue int64 = 0
        arr := make([]int64, n+1)
        
        for _, query := range queries {
            arr[query[0]-1] += int64(query[2])
            arr[query[1]] -= int64(query[2])
        }
        
        var currVal int64 = 0
        for _, v := range arr {
            currVal += v
            if currVal > maxValue {
                maxValue = currVal
            }
        }
        
        return maxValue
    }