c++ | take and notTake dp | memoization | simple and easy to understand
Complexity\n- Time complexity:\no(nk)\n\n- Space complexity:\no(nk)\n\n# Code\n\nclass Solution {\nprivate:\n    int solve(int ind, vector<vector<int>>& piles, 
# Complexity\n- Time complexity:\no(n*k)\n\n- Space complexity:\no(n*k)\n\n# Code\n```\nclass Solution {\nprivate:\n    int solve(int ind, vector<vector<int>>& piles, vector<vector<int>>& dp, int k) {\n        if(ind >= piles.size()) return 0;\n\n        if(dp[ind][k] != -1) return dp[ind][k];\n\n        int sum = 0, take = 0;\n\n        //not taking from current pile;\n        int notTake = 0 + solve(ind+1,piles,dp,k);\n\n        //taking from current pile if we can take according to given k and move towards next ind\n        for(int i=0;i<piles[ind].size();i++) {\n            sum += piles[ind][i];\n\n            if(k - (i+1) >= 0) {\n                take = max(take, sum + solve(ind+1, piles, dp, k-(i+1)));\n            }\n        }\n\n        return dp[ind][k] = max(take, notTake);\n    }\n\npublic:\n    int maxValueOfCoins(vector<vector<int>>& piles, int k) {\n        int n = piles.size();\n        vector<vector<int>> dp(n+1, vector<int>(k+1, -1));\n        return solve(0,piles,dp,k);\n    }\n};\n```
['C++']