You are viewing a single comment's thread. Return to all 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;
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 →
Java
public static int maximumToys(List prices, int k) {
Collections.sort(prices);
int totalPrice = 0; int numToBuy = 0;
while(totalPrice <= k){
} //subtracting 1 since while loop will run one additional time when the value should not be added before ending return numToBuy-1;