Mark and Toys Discussions | Algorithms | HackerRank

Mark and Toys

  • + 1 comment

    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:

    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)
    }