Priyanka and Toys Solution in JS
Priyanka and Toys Solution
Hackerrank.com
solution in Javascript
function processData(input) {
input = input.split('\n');
let n = parseInt(input[0]);
let toys = input[1].split(' ').map(Number).sort((a,b)=>{return a-b;});
toys = [...new Set(toys)]; // unique items
n = toys.length;
var i =0;
var buckets=1;
curBucket = toys[0];
for( var i=1; i<n; i++){
if(curBucket+4<toys[i]){
buckets++;
curBucket = toys[i];
}
}
console.log(buckets);
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Σχόλια