750. Ordered Ways to Total

Medium3.5 points estimatedArrayDynamic Programming3 hints

A dance troupe builds a routine out of moves. Move i lasts nums[i] beats, all durations are different, and any move may be performed as many times as the choreographer likes. The routine must last exactly target beats.

Return the number of different routines, where a routine is the ordered list of moves performed one after another. The same moves in a different order count as different routines, so [1, 2] and [2, 1] are two routines for 3 beats. A routine of 0 beats is the empty one, giving exactly 1 way when target is 0.

The count can be very large, so return it modulo 1000000007. Aim for time O(n * target) and space O(target).

Example 1

Input:
nums = [2,3,4], target = 7
Output:
5
Explanation:

The moves 2, 2, 3 can be ordered in 3 ways, and 3, 4 in 2 ways, giving 5 routines of 7 beats.

Example 2

Input:
nums = [4,6], target = 5
Output:
0
Explanation:

No combination of 4-beat and 6-beat moves adds up to 5, so there are no routines.

Constraints

1 ≤ nums.length ≤ 100

1 ≤ nums[i] ≤ 300, all values are distinct

0 ≤ target ≤ 10000

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms

Expected complexity

Time
O(n * target)
Space
O(target)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…