Easy DP Solution in Java using Recursion and Memoization
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();

        int[][] dp=new int[n][target+1];

        for(int[] row:dp)
        {
            Arrays.fill(row,-1);
        } 

        int ans=f(0,nums,target,dp);
        if(ans<=0)
        {
            return -1;
        }
        return ans;
        
    }

    public int f(int index, List<Integer> nums, int target, int[][] dp)
    {
        if(target==0)
        {
            return 0;
        }

        if(index==nums.size())
        {
            return -1000;
        }

        if(dp[index][target]!=-1)
        {
            return dp[index][target];
        }

        int take=Integer.MIN_VALUE;
        
        if(nums.get(index)<=target)
        {
            take=1+f(index+1,nums,target-nums.get(index),dp);
        }

        int ntake=f(index+1,nums,target,dp);
        dp[index][target]=Math.max(take,ntake);
        return dp[index][target];
    }
}
```
['Dynamic Programming', 'Recursion', 'Java']