We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here's the same solution in swift in case anyone needs it :).
func arrayManipulation(n: Int, queries: [[Int]]) -> Int {
var sums = Int: Int
for query in queries {
sums[query[0]] = (sums[query[0]] ?? 0) + query[2]
sums[query[1] + 1] = (sums[query[1] + 1] ?? 0) - query[2]
}
var currentmax = 0
var sum = 0
sums.sorted{ `$0.0 < $`1.0 }.
compactMap{ `$0.1; sum += $`0.1; currentmax = sum > currentmax ? sum : currentmax}
return currentmax
}
Cookie support is required to access HackerRank
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 →
Here's the same solution in swift in case anyone needs it :).
func arrayManipulation(n: Int, queries: [[Int]]) -> Int { var sums = Int: Int for query in queries { sums[query[0]] = (sums[query[0]] ?? 0) + query[2] sums[query[1] + 1] = (sums[query[1] + 1] ?? 0) - query[2] }
}