dp
of size target + 1
with all elements set to 0.nums
.target - 1
down to the number itself.dp[j]
is not zero (indicating a subsequence of length dp[j]
sums up to j
), and j + nums[i]
is less than or equal to target
, update dp[j + nums[i]]
to be the maximum of its current value and dp[j] + 1
.dp[target]
is zero, return -1
, indicating no subsequence sums up to target
. Otherwise, return dp[target]
.