dp
with size k
and set dp[0]
to 1 (base case: one way to achieve sum 0).nums
and for each element num
, iterate backwards from k-1
to num
, updating dp[i]
by adding dp[i - num]
to it.2^n
, where n
is the number of elements in nums
.dp
array for sums less than k
.10^9 + 7
.