Skip to the code
cf.
+25 XP

#303 Reusable Coupon Totals

medium

A checkout page accepts store coupons. values lists the distinct coupon values, and a customer may use each value any number of times.

Write couponMixes(values, target) that returns every distinct mix of coupons that adds up to exactly target. Write each mix as an array sorted ascending. The mixes may be listed in any order.

Two mixes are the same if they use the same values the same number of times. Return [] if no mix works.

Example 1

Input: values = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]

Each mix adds to 8. [3, 5] and [5, 3] are the same mix, so it appears once.

Example 2

Input: values = [4], target = 2
Output: []

The only coupon is worth more than the target.

Constraints

  • 1 <= values.length <= 10
  • 1 <= values[i] <= 40, all distinct
  • 1 <= target <= 40
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).