Skip to the code
cf.
+10 XP

#570 Most Items Within a Gift Budget

easy

A wishlist app has a "stretch my budget" button. Given the prices of the items on a wishlist, prices, and the amount of money available, budget, it reports how many items the user could buy at most.

Write mostItems(prices, budget) that returns the largest number of different items whose prices add up to at most budget. Each item can be bought once.

Example 1

Input: prices = [8,3,12,5,1,9], budget = 20
Output: 4

Buy 1, 3, 5, and 8 for 17. Any five items cost at least 26.

Example 2

Input: prices = [30,25,40], budget = 10
Output: 0

Even the cheapest item is over budget.

Constraints

  • 0 <= prices.length <= 100000
  • 1 <= prices[i] <= 10^5
  • 0 <= budget <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).