Skip to the code
cf.
+25 XP

#296 Closest Bundle Price

medium

A gift shop page lets a buyer pick any set of items from prices, each item at most once, including no items at all (total 0).

Write closestBundleTotal(prices, target) that returns the total of the set whose total is closest to target. If two totals are equally close, return the smaller total.

Example 1

Input: prices = [4,9,6], target = 12
Output: 13

The possible totals are 0, 4, 6, 9, 10, 13, 15, 19. 13 is 1 away from 12, closer than 10, which is 2 away.

Example 2

Input: prices = [5,5], target = 7
Output: 5

Totals 0, 5, and 10. 5 is 2 away and 10 is 3 away.

Constraints

  • 1 <= prices.length <= 15
  • 1 <= prices[i] <= 1000
  • 0 <= target <= 15000
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).