Skip to the code
cf.
+10 XP

#517 Fewest Coupons for a Discount

easy

A checkout page lets shoppers stack coupons. Each coupon type in coupons takes a fixed number of dollars off, and the shopper has as many coupons of each type as they like.

Write fewestCoupons(coupons, discount) that returns the fewest coupons whose values add up to exactly discount. If no combination does, return -1. A discount of 0 needs 0 coupons.

Example 1

Input: coupons = [1,3,4], discount = 6
Output: 2

3 + 3. Taking the biggest coupon first (4 + 1 + 1) uses three.

Example 2

Input: coupons = [5,10], discount = 3
Output: -1

Every combination is a multiple of 5.

Constraints

  • 1 <= coupons.length <= 12
  • 1 <= coupons[i] <= 10000
  • 0 <= discount <= 10000
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).