95.97% Faster, Memory 99.36%. Compact Knapsack Solution with O(N) SC
Intuition\n\nIt\'s a variation of Classical Knapsack Problem, I learned this method from Erricto (Attached Timestamp), Very Ingenious way, DO Check it Out for m
# Intuition\n\nIt\'s a variation of Classical Knapsack Problem, I learned this method from Erricto (Attached Timestamp), Very Ingenious way, DO Check it Out for more Understanding   \n\n[Erricto Atcoder DP Contest](https://www.youtube.com/watch?v=FAQxdm0bTaw&t=1666s)\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:   $$O(n*target)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:  $$O(target)$$ \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int lengthOfLongestSubsequence(vector<int>& nums, int target) \n    {\n        int n=nums.size();\n\n        int dp[target+1];\n\n        for(int i=0;i<=target;i++)\n        {\n            dp[i]=-1e6;\n        }\n\n        // knapsack - weights, values, limit\n        // this  - nums, 1 , target\n\n        dp[0]=-1;\n\n        for(int i=0;i<n;i++)\n        {\n            for(int j=target;j>=nums[i];j--)\n            {\n                dp[j] = max(dp[j],1+dp[j-nums[i]]);\n            }\n        }\n        \n        return (dp[target]<-1?(-1):(dp[target]+1));\n    }\n};\n```
['Dynamic Programming', 'C++']