AlgoFrames

Combination sum IV

Challenge · Write code · about 18 min

nums holds distinct positive numbers. Return how many ordered sequences of numbers from nums add up to target. Numbers may repeat, and different orders count separately, so 1 + 2 and 2 + 1 are two sequences.

Example 1

Input:

[[1,2,3],4]

Output:

7

1111, 112, 121, 211, 22, 13, 31.

Example 2

Input:

[[9],3]

Output:

0

9 is too big.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 1000, all different
  • 1 ≤ target ≤ 1000
  • The answer fits in a 32-bit signed integer

Learn the idea first: Coin change: minimum coins and number of ways, a visual lesson.

More Coin change: minimum coins and number of ways problems