2915. Length of the Longest Subsequence That Sums to Target
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int lengthOfLongestSubsequence(vector<int>& nums, int target) {
        int n = nums.size();
        vector<int> dp(target + 1, -1); 
        dp[0] = 0; 
        
        for (int i = 0; i < n; i++) {
            for (int j = target; j >= nums[i]; j--) {
                if (dp[j - nums[i]] != -1) {
                    dp[j] = max(dp[j], dp[j - nums[i]] + 1);
                }
            }
        }
        
        return dp[target] == -1 ? -1 : dp[target];
    }
};

```
['C++']