You are viewing a single comment's thread. Return to all comments →
Same solution in scala
def arrayManipulation(n: Int, queries: Array[Array[Int]]): Long = { val arr = Array.fill[Int](n)(0) queries foreach { case Array(a, b, k) => arr(a - 1) = arr(a - 1) + k if (b < n) arr(b) = arr(b) - k } val max = 0L val sum = 0L val (result, _) = arr.foldLeft((max, sum)) { case ((currentMax, currentSum), elem) => val newSum = currentSum + elem if (newSum > currentMax) (newSum, newSum) else (currentMax, newSum) } result }
Seems like cookies are disabled on this browser, please enable them to open this website
Array Manipulation
You are viewing a single comment's thread. Return to all comments →
Same solution in scala