short DP code
Time complexity:
O(NT)
Space complexity:
O(T)
Code
- Time complexity:
$$O(NT)$$
- Space complexity:
$$O(T)$$

# Code
```cpp []
class Solution {
public:
    int lengthOfLongestSubsequence(vector<int>& nums, int target) {
        vector<int> dp(target+1, INT_MIN);
        dp[0] = 0;

        for (int x : nums){
            for (int i = target-x; i >= 0; i--){
                dp[i+x] = max(dp[i] + 1, dp[i+x]);
            }
        }

        return dp[target] < 0 ? -1 : dp[target];
    }
};
```
['C++']