Mark and Toys Discussions | Algorithms | HackerRank

Mark and Toys

  • + 0 comments

    Java

    public static int maximumToys(List prices, int k) {

    Collections.sort(prices);

    int totalPrice = 0; int numToBuy = 0;

    while(totalPrice <= k){

    totalPrice = totalPrice + prices.get(numToBuy);
    
    numToBuy++;
    

    } //subtracting 1 since while loop will run one additional time when the value should not be added before ending return numToBuy-1;