C++ | DP + MEMOIZATION, you can refer:)
IntuitionApproachComplexity
Time complexity: O(n * target)

Space complexity: O(n * target)
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

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

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

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

# Code
```cpp []
class Solution {
public:
    vector<vector<int>>dp;
    int solve(int i, int tar, vector<int>& nums) {
        if(tar == 0) return 0;
        if(i >= nums.size() || tar < 0) return -1e9;
        if(dp[i][tar] != -1) return dp[i][tar];
        int take = solve(i + 1, tar - nums[i], nums);
        if(take != -1e9) take = take + 1;
        int nottake = solve(i + 1, tar, nums);
        return dp[i][tar] = max(take, nottake);
    }
    int lengthOfLongestSubsequence(vector<int>& nums, int target) {
        dp.assign((int)nums.size(), vector<int>(target + 1, -1));
        int f = solve(0, target, nums);
        return f < 0 ? -1 : f;
    }
};
```
['C++']