🔥Recursion + Memoization | Clean Code | C++ |
Code\n\nclass Solution {\npublic:\n    int rec(int i, int sum, vector<int>& nums, vector<vector<int>>& dp)\n    {\n        if (sum == 0)\n            return 1;\
# Code\n```\nclass Solution {\npublic:\n    int rec(int i, int sum, vector<int>& nums, vector<vector<int>>& dp)\n    {\n        if (sum == 0)\n            return 1;\n        if (i >= nums.size())\n            return 0;\n        \n        if (dp[i][sum] != -1)\n            return dp[i][sum];\n\n        int notTake = rec(i + 1, sum, nums, dp);\n        int take = 0;\n        if (nums[i] <= sum)\n        {\n            int tmp = rec(i + 1, sum - nums[i], nums, dp);\n            if (tmp != 0)\n                take = tmp + 1;\n        }\n        return dp[i][sum] = max(take, notTake);\n    }\n    int lengthOfLongestSubsequence(vector<int>& nums, int target) {\n        vector<vector<int>> dp(nums.size() + 1, vector<int>(target + 1, -1));\n        return rec(0, target, nums, dp) - 1;\n    }\n};\n```
['C++']