bottom-up / top-down
Top-down, memory limit exceeded:
```python3 []
class Solution:
    def lengthOfLongestSubsequence(self, nums: List[int], target: int) -> int:
        dp = [[(0, False) for _ in range(target + 1)] for _ in nums]
        for i, num in enumerate(nums):
            if i == 0:
                if num <= target:
                    dp[i][num] = (1, True)
                continue
            for t in range(target + 1):
                if t < num:
                    dp[i][t] = dp[i - 1][t]
                elif t == num:
                    max_length = 1
                    prev_max_length, prev_can_do = dp[i - 1][t]
                    if prev_can_do:
                        max_length = max(max_length, prev_max_length)
                    dp[i][t] = (max_length, True)
                else:
                    max_length_1, can_do_1 = dp[i - 1][t - num]
                    max_length_1 += 1
                    max_length_2, can_do_2 = dp[i - 1][t]
                    if not can_do_1 and not can_do_2:
                        dp[i][t] = (0, False)
                    else:
                        max_length = 0
                        if can_do_1:
                            max_length = max(max_length, max_length_1)
                        if can_do_2:
                            max_length = max(max_length, max_length_2)
                        dp[i][t] = (max_length, True)

        max_len, can_do = dp[-1][target]
        return -1 if not can_do else max_len
```


Top-down, memory limit exceeded:

```python
# top-down, memory limit exceeded
class Solution:
    def lengthOfLongestSubsequence(self, nums: List[int], target: int) -> int:
        cache = {}
        def dp(i, t):
            if i == 0:
                return (1, True) if t == nums[0] else (0, False)

            if (i, t) in cache:
                return cache[(i, t)]

            if t < nums[i]:
                return dp(i - 1, t)
            
            elif t == nums[i]:
                max_length = 1
                prev_max_length, prev_can_do = dp(i - 1, t)
                if prev_can_do:
                    max_length = max(max_length, prev_max_length)
                cache[(i, t)] = (max_length, True)
            
            else:
                max_length_1, can_do_1 = dp(i - 1, t - nums[i])
                max_length_1 += 1
                max_length_2, can_do_2 = dp(i - 1, t)
                if not can_do_1 and not can_do_2:
                    cache[(i, t)] = (0, False)
                else:
                    max_length = 0
                    if can_do_1:
                        max_length = max(max_length, max_length_1)
                    if can_do_2:
                        max_length = max(max_length, max_length_2)
                    cache[(i, t)] = (max_length, True)
            
            return cache[(i, t)]
        
        max_len, can_do = dp(len(nums) - 1, target)
        return -1 if not can_do else max_len
```
['Python3']