Skip to content
Onsite

Combination Sum III

MediumP3Coding

Betty has nine coins with values 1 to 9, one of each. She wants to pay exactly p using exactly k different coins.

Return every set of k distinct values from 1..9 that sum to p. Write each set in increasing order and list the sets in lexicographic order. If no set exists, return an empty array.

Example 1

Input
k = 3, p = 7
Output
[[1,2,4]]
Why
Only {1, 2, 4} uses three coins to make 7.

Example 2

Input
k = 3, p = 9
Output
[[1,2,6],[1,3,5],[2,3,4]]
Why
Three sets: {1,2,6}, {1,3,5} and {2,3,4}.

Example 3

Input
k = 4, p = 1
Output
[]

Constraints

  • 1 <= k <= 9
  • 1 <= p <= 60

Hints

Run checks the 3 visible tests, Submit all 10.