Easy DP solution | CPP
Intuition\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 
# Intuition\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:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> dp;\n    int lengthOfLongestSubsequence(vector<int>& nums, int t) {\n        int n=nums.size();\n        dp.resize(n+1,vector<int>(t+1,-2));\n        int ans=rec(nums,n,t,0,0);\n        return ans;\n    }\n    int rec(vector<int>& nums,int n,int t,int idx,int sum)\n    {\n        if(sum==t){\n            return 0;\n        }\n        if(idx>=n || sum>t)return -1; // return -1 as we added 1 to res. This cancel out that 1+.\n        if(dp[idx][sum]!=-2)return dp[idx][sum];\n        int res=0;\n        res=max(res,rec(nums,n,t,idx+1,sum));// not pick\n        res = max(res,1+rec(nums, n, t, idx + 1, sum + nums[idx]));// pick. As above discussed, if the pick rec gives -1 it cancel out that 1 +\n        return dp[idx][sum]=res==0?-1:res;// If current rec gives 0 then return -1 which cancel outs the prev res incrementations.\n    }\n    \n};\n```
['C++']