-
-
Save blackout314/6593bca74ba34b74b1249b03df78e6f5 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 hidden or 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); | |
} | |
for(i=0; i<m; i++) { | |
op = ops[i]; | |
for(j=op[0]-1; j < op[1]; j++) { | |
list[j] = list[j] + op[2]; | |
} | |
} | |
max = list[0]; | |
for(i=1; i<n; i++) { | |
if(list[i] > max) { | |
max = list[i]; | |
} | |
} | |
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