Mark and Toys Discussions | Algorithms | HackerRank

Mark and Toys

Sort by

recency

|

1046 Discussions

|

  • + 0 comments

    Is it cheating to use an inbuilt sort here? It seems like the whole point is to sort it yourself

  • + 0 comments
    function maximumToys(prices, k) {
        const sortedPrices = prices.sort((a, b)=> a-b)
        let total = 0
        let count =0
        while((total + sortedPrices[count]) < k){
            total +=sortedPrices[count]
            count++
        }
        return count
    }
    
  • + 0 comments

    Java

    	Collections.sort(prices);
            int sum = 0;
            int count = 0;
            for(Integer i : prices){
                sum += i;
                if(sum > k){
                    break;
                } else{
                    count++;
                }
            }
            return count;
    
  • + 0 comments
    def maximumToys(prices, k):
        # Write your code here
        prices.sort()
        current=0
        toys=0
        for toy in prices:
            if toy+current<=k:
                current+=toy 
                toys+=1
        return toys
    
  • + 0 comments

    Java:

    public static int maximumToys(List<Integer> prices, int k) {
        prices.sort(Comparator.naturalOrder());
        int i = 0;
        int totalSpend = 0;
        for (;i < prices.size() && totalSpend < k; ++i) {
            totalSpend += prices.get(i);
        }
        return i-1;
    }