-
-
Save kkoch986/21e8bee5fc87743b1f22 to your computer and use it in GitHub Desktop.
Naive javascript solution for Algorithmic Crush -- https://www.hackerrank.com/contests/w4/challenges/crush
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
String.prototype.splitSpacesAsInts = function() { | |
return this.split(" ").map(function(aNum) { return parseInt(aNum); }); | |
}; | |
function processData(input) { | |
var lines = input.split("\n"); | |
var first = lines[0].splitSpacesAsInts(); | |
var n = first[0]; | |
var m = first[1]; | |
var ops = lines.slice(1).map(function(line) { return line.splitSpacesAsInts(); }); | |
var max = crush(n, m, ops); | |
console.log(max); | |
} | |
function crush(n, m, ops) { | |
var list = [], op, max, i, j; | |
for(i=0; i<n; i++) { | |
list.push(0); | |
} | |
max = 0; | |
for(i=0; i<m; i++) { | |
op = ops[i]; | |
for(j=op[0]-1; j < op[1]; j++) { | |
list[j] = list[j] + op[2]; | |
if(list[j] > max) { | |
max = list[j]; | |
} | |
} | |
} | |
return max; | |
} | |
process.stdin.resume(); | |
process.stdin.setEncoding("ascii"); | |
_input = ""; | |
process.stdin.on("data", function (input) { | |
_input += input; | |
}); | |
process.stdin.on("end", function () { | |
processData(_input); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment