Skip to content

Instantly share code, notes, and snippets.

@kkoch986
Forked from mbuff24/crush.js
Last active December 29, 2015 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkoch986/21e8bee5fc87743b1f22 to your computer and use it in GitHub Desktop.
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
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