Python 3 || 6 lines, iterative dp, w/example || T/S: 98% / 51%
\nclass Solution:\n    def lengthOfLongestSubsequence(self, nums: List[int],   # Ex: nums = [4,1,3,2,1]  target = 7\n                                    target:
```\nclass Solution:\n    def lengthOfLongestSubsequence(self, nums: List[int],   # Ex: nums = [4,1,3,2,1]  target = 7\n                                    target: int) -> int:\n                                                            #  num  dp \n                                                            #  ---  ------------\n        (dp:= [0]).extend([-1] * target)                    #  -->   [0,-1,-1,-1, 1,-1,-1,-1]\n        for num in nums:                                    #   4    [0,-1,-1,-1, 1,-1,-1,-1]\n            for i in reversed(range(num, target+1)):        #   1    [0, 1,-1,-1, 1, 2,-1,-1]\n                nxt = dp[i - num] + 1                       #   3    [0, 1,-1, 1, 2, 2,-1, 2]\n                dp[i] = max(nxt, dp[i]) if nxt else dp[i]   #   2    [0, 1,-1, 1, 2, 2,-1, 2]\n                                                            #   1    [0, 1, 2, 2, 3, 3, 3, 4]\n        return dp[-1]                                       #                               \\_ return 4\n```\n[https://leetcode.com/problems/length-of-the-longest-subsequence-that-sums-to-target/submissions/1126127628/](http://)\n\n\n\nI could be wrong, but I think that time complexity is *O*(*NT*) and space complexity is *O*(*T*), in which *N* ~ `len(nums)` and *T* ~ `target`.
['Python3']