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.
Input: coupons = [1,3,4], discount = 6 Output: 2
3 + 3. Taking the biggest coupon first (4 + 1 + 1) uses three.
Input: coupons = [5,10], discount = 3 Output: -1
Every combination is a multiple of 5.
1 <= coupons.length <= 121 <= coupons[i] <= 100000 <= discount <= 10000Run 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).
I'm here to help with #517 Fewest Coupons for a Discount. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.