• + 0 comments

    Thanks for the enlightenment.

    Here is the same solution in Swift:

    func arrayManipulation(n: Int, queries: [[Int]]) -> Int {
    
        var numbers = Array(repeating: 0, count: n + 1)
        var ans = 0
    
        for i in 0 ..< queries.count {
            var a = queries[i][0]
            var b1 = queries[i][1] + 1
            var k = queries[i][2]
    
            numbers[a] += k
            
            if b1 <= n {
                numbers[b1] -= k
            }
        }
    
        for i in 1...n {
            numbers[i] += numbers[i - 1]
    				
            if numbers[i] > ans {
                ans = numbers[i]
            }
        }
    		
        return ans
    }