Created
September 24, 2020 22:52
-
-
Save Gosilama/0ad10d4341927a2e48151b1514beaeb3 to your computer and use it in GitHub Desktop.
Solution to https://www.hackerrank.com/challenges/crush/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays array problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arrayManipulation = (n, queries) => { | |
const zeroArr = []; | |
let max = 0; | |
for (let i = 0; i < n; i++) { | |
zeroArr.push(0); | |
} | |
queries.forEach((query) => { | |
zeroArr[query[0] - 1] += query[2]; | |
if (query[1] < n) { | |
zeroArr[query[1]] -= query[2]; | |
} | |
}); | |
for (let i = 1; i < n; i++) { | |
zeroArr[i] += zeroArr[i - 1]; | |
} | |
zeroArr.forEach(value => { | |
max = max > value ? max : value; | |
}); | |
return max; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment