Mark and Toys Discussions | Algorithms | HackerRank

Mark and Toys

  • + 2 comments

    javascript solution:

    function maximumToys(prices, k) {
        let total = 0;
        let toys = [];
        let sortedPrices = prices.sort((a, b) => a - b);
    
        for (let p of sortedPrices) {
            if (total + p <= k) {
                toys.push(p);
                total += p;
            } else {
                return toys.length;
            }
        }
    }