21 / 75
M

Combination Sum IV

#21
dynamic-programmingarray

Count number of combinations that sum to target where order matters (permutations).

Example:

Input:nums=[1,2,3], target=4
Output:7 (ways: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2, 1+3, 3+1)

Common Mistakes:

  • Confusing with Combination Sum (order doesn't matter vs order matters)
  • Wrong loop order - target must be outer loop for permutations
  • Integer overflow - should check or use long

Notes:

Order matters = permutations, so target is outer loop. Time O(target * nums.length), Space O(target). Similar to Coin Change but counting ways.

21/75
Combination Sum IV