Using DP
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
```java []
class Solution {
    public int lengthOfLongestSubsequence(List<Integer> nums, int target) {
        int n=nums.size();
        long [][]dp=new long[n][target+1];
        for(int i=0;i<dp.length;i++){
            Arrays.fill(dp[i],-1);
        }
        int ans=(int)countTarget(0,nums,target,dp);
        if(ans<0){
            return -1;
        }
        else return ans;
    }
    public long countTarget(int i,List<Integer> nums, int target,long [][]dp){
        if(i==nums.size()){
            if(target==0)return 0;
            else return Integer.MIN_VALUE;
        }
        if(dp[i][target]!=-1)return dp[i][target];
        long skip=countTarget(i+1,nums,target,dp);
        if(target-nums.get(i)<0)return skip;
        long pick=1+countTarget(i+1,nums,target-nums.get(i),dp);
        return dp[i][target]=Math.max(skip,pick);
    }
}
```
['Dynamic Programming', 'Java']