Combination Sum IV

Given an array of distinct positive integers and a target, return the number of ordered combinations that sum to the target. This is the classic iterate-order problem where changing loop order changes the result.

Input Format

nums = distinct positive integers, target = required sum

Output Format

number of ordered combinations that sum to target

Constraints

  • 1 <= nums.length <= 200; 1 <= nums[i] <= 1000; 0 <= target <= 1000

Examples

Example 1:

Input:

nums = [1,2,3]
target = 4

Output:

7

Explanation:

The ordered sequences are [1,1,1,1], [1,1,2], [1,2,1], [2,1,1], [2,2], [1,3], and [3,1].

Example 2:

Input:

nums = [9]
target = 3

Output:

0

Explanation:

No ordered combination can sum to 3 using only 9.

Loading...
Combination Sum IV - Dp Knapsack DSA Problem