Memo
IntuitionApproachComplexity

Time complexity:
O(nlogn)


Space complexity:
O(n)

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

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

# Complexity
- Time complexity:
O(nlogn)

- Space complexity:
O(n)

# Code
```java []
class Solution {
    int backtrack(List<Integer> nums, int target, int idx, int len, int[][] memo) {
        if (target == 0) {
            return len;
        }
        if (target < 0 || idx == nums.size()) {
            return -1;
        }
        if (memo[idx][target] != 0) {
            return len + memo[idx][target] - 1;
        }
        int res = Math.max(
            backtrack(nums, target - nums.get(idx), idx + 1, len + 1, memo),
            backtrack(nums, target, idx + 1, len, memo)
        );
        memo[idx][target] = res - len + 1;
        return res;
    }

    public int lengthOfLongestSubsequence(List<Integer> nums, int target) {
        return backtrack(nums, target, 0, 0, new int[nums.size()][target + 1]);
    }
}
```
['Java']