2D DP sort first.
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[][] dp = new int[nums.size()][target + 1];
        Collections.sort(nums, (a, b) -> a - b);
        int res = helper(nums, 0, target, dp);
        return res < 0 ? -1 : res;        
    }

    public int helper(List<Integer> nums, int index, int target, int[][] dp ) {
        if (index == nums.size() || nums.get(index) > target) {
            return Integer.MIN_VALUE;
        }

        if (nums.get(index) == target) {
            return 1;
        }

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

        int res = 0;
        
        res = Math.max(helper(nums, index + 1, target, dp), 1 + helper(nums, index + 1, target - nums.get(index), dp));

        dp[index][target] = res;

        return res;

    }
}
```
['Java']