Created
May 21, 2020 22:49
-
-
Save rakibulalam/b019e02f7ac0e688664240b5f9281680 to your computer and use it in GitHub Desktop.
Mark and Toys Hacker Rank
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
'use strict'; | |
const fs = require('fs'); | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
let inputString = ''; | |
let currentLine = 0; | |
process.stdin.on('data', inputStdin => { | |
inputString += inputStdin; | |
}); | |
process.stdin.on('end', _ => { | |
inputString = inputString.replace(/\s*$/, '') | |
.split('\n') | |
.map(str => str.replace(/\s*$/, '')); | |
main(); | |
}); | |
function readLine() { | |
return inputString[currentLine++]; | |
} | |
// Complete the maximumToys function below. | |
function maximumToys(prices, k) { | |
const n = prices.length; | |
let items = 0, spend = 0; | |
prices.sort((a,b)=>a-b).every((v,i)=>{ | |
spend+=v; | |
items=i; | |
return spend<=k; | |
}); | |
return items; | |
} | |
function main() { | |
const ws = fs.createWriteStream(process.env.OUTPUT_PATH); | |
const nk = readLine().split(' '); | |
const n = parseInt(nk[0], 10); | |
const k = parseInt(nk[1], 10); | |
const prices = readLine().split(' ').map(pricesTemp => parseInt(pricesTemp, 10)); | |
let result = maximumToys(prices, k); | |
ws.write(result + "\n"); | |
ws.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment