You are viewing a single comment's thread. Return to all comments →
I've added a little optimisation to filter out elements above the budget, that we don't need spending time ordering.
public static int maximumToys(List<Integer> prices, int k) { var sorted = prices.stream() .filter(p -> p <= k) .sorted() .collect(Collectors.toList()); var i = 0; var budget = k; while(i < sorted.size() && budget >= sorted.get(i)) { budget -= sorted.get(i); ++i; } return i; }
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 →
I've added a little optimisation to filter out elements above the budget, that we don't need spending time ordering.