Easy Understanding DP + Memoziation Solution
UPVOTE IF YOU LIKE THE SOLUTION.\n\nclass Solution {\npublic:\n    //memoization Solution\n    int helper(int i, vector<vector<int>>& piles, int k, vector<vecto
UPVOTE IF YOU LIKE THE SOLUTION.\n```\nclass Solution {\npublic:\n    //memoization Solution\n    int helper(int i, vector<vector<int>>& piles, int k, vector<vector<int>>& dp){\n        //base condition\n        if(i == piles.size() || k <= 0){\n            return 0;\n        }\n        \n        //check if this condition is reached before\n        if(dp[i][k] != -1){\n            return dp[i][k];\n        }\n        \n        long long ans = INT_MIN;\n        for(int j = 0; j < piles[i].size(); j++){\n            //if j becomes greater than k-1 break\n            if(j > k-1)\n                break;\n            \n            //take starting j+1 coins from the ith pile\n            long long take = piles[i][j] + helper(i+1, piles, k-j-1, dp);\n            \n            //store the max value in ans\n            ans = max(ans, take);\n        }\n        \n        //donot take any coins from ith piles\n        long long ntake = helper(i+1, piles, k, dp);\n        ans = max(ans, ntake);\n        \n        //memoize it\n        return dp[i][k] = ans;\n    }\n    int maxValueOfCoins(vector<vector<int>>& piles, int k) {\n        //dp vector\n        vector<vector<int>> dp(piles.size(), vector<int>(k+1,-1));\n        \n        //prefix vector\n        vector<vector<int>> prefix;\n        for(auto a: piles){\n            int n = a.size();\n            vector<int> temp(n);\n            temp[0] = a[0];\n            for(int i = 1; i < n; i++){\n                temp[i] = temp[i-1] + a[i];\n            }\n            prefix.push_back(temp);\n        }\n        return helper(0, prefix, k, dp);\n    }\n};\n```
['Dynamic Programming', 'Recursion', 'Memoization', 'C']