Quant Memo
Coding/●●●●●

Count ordered ways to reach a target

Given distinct positive integers nums and a target, count the number of ordered sequences drawn from nums (with repetition) that sum to the target. Because order matters, 1+3 and 3+1 count as two 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)

Return the count. Target O(target×#nums)O(\text{target} \times \#\text{nums}).

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions