Explained Solution by relating it to coin change problem || Memoisation || C++
Intution and Relation with Coin Change\nLets try to recall what do we do in coin change , we go to every index and either pick or not pick the element.\nSimilar
### **Intution and Relation with Coin Change**\nLets try to recall what do we do in coin change , we go to every index and either pick or not pick the element.\nSimilarly , in this question , we go to every index and because we can pick atmost k elements so , we try all the possibilities of picking elements from 0 to min(piles[idx].size() , k ) and we have to take max of all the possibilities \nwe tried and return . As simple as that. For the memoisation part , the two parameters that are changing are memoised to reduce the \n**Time complexity from O(K^n) to (O(k*n)**\nIf you dont understand any part , try to recall coin change problem thoroughly or comment below. I would be happy to explain further.\n\n\n```\n    int fun(vector<vector<int>>& piles, vector<vector<int>>& dp , int idx , int k)\n    {\n        if(idx == piles.size() || k==0) return 0;\n        \n        if(dp[idx][k]!= -1) return dp[idx][k] ;\n        \n        int ans = fun(piles, dp , idx+1 , k ) ;\n        int sum = 0;\n        \n        for(int i=0; i< min(int(piles[idx].size()) , k) ; i++)\n        {\n            sum+=piles[idx][i] ;\n            ans = max(ans , sum + fun(piles , dp , idx+1 , k-i-1)) ;\n        }\n        \n        return dp[idx][k] = ans ;\n        \n    }\n    \n    int maxValueOfCoins(vector<vector<int>>& piles, int k) {\n         \n         int n = piles.size();\n         vector<vector<int> > dp(n+1 , vector<int>(k+1 , -1)) ;\n      \n        return fun(piles , dp , 0 , k) ;\n        \n        \n    }
['Dynamic Programming', 'Memoization', 'C']