Mark and Toys Discussions | Algorithms | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My only suggestion would be to avoid using an array if possible.
Pushing all those numbers is unnecessarily expensive!
We basically did the same thing though:
function maximumToys(prices, k) {
let sum = 0;
let sorter = prices.sort((a, b) => a - b);
let counter = 0
for (var i = 0; i < sorter.length; i++){
if (sorter[i] < k){
sum += sorter[i]
if (sum <= k){
counter++
}
}
}
console.log(counter)
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Mark and Toys
You are viewing a single comment's thread. Return to all comments →
Not a bad solution!
My only suggestion would be to avoid using an array if possible.
Pushing all those numbers is unnecessarily expensive!
We basically did the same thing though: