Skip to content

Combination Sum IV

MediumDynamic Programming
Decode it first. Clues, constraints, pattern and plan before any code.

Given an array of distinct positive integers nums and a target, return how many ordered sequences of numbers from nums add up to target. Numbers may be used any number of times, and different orders count as different sequences.

nums = [1, 2, 3], target = 4  →  7
  (1,1,1,1) (1,1,2) (1,2,1) (2,1,1) (2,2) (1,3) (3,1)
nums = [9], target = 3        →  0

Constraints

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 1000
  • 1 <= target <= 1000
  • The answer fits in a 32-bit integer.