{"id":9596,"date":"2022-03-28T21:51:27","date_gmt":"2022-03-29T04:51:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9596"},"modified":"2022-03-29T02:11:43","modified_gmt":"2022-03-29T09:11:43","slug":"leetcode-2218-maximum-value-of-k-coins-from-piles","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-2218-maximum-value-of-k-coins-from-piles\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2218. Maximum Value of K Coins From Piles"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;<strong>piles<\/strong>&nbsp;of coins on a table. Each pile consists of a&nbsp;<strong>positive number<\/strong>&nbsp;of coins of assorted denominations.<\/p>\n\n\n\n<p>In one move, you can choose any coin on&nbsp;<strong>top<\/strong>&nbsp;of any pile, remove it, and add it to your wallet.<\/p>\n\n\n\n<p>Given a list&nbsp;<code>piles<\/code>, where&nbsp;<code>piles[i]<\/code>&nbsp;is a list of integers denoting the composition of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;pile from&nbsp;<strong>top to bottom<\/strong>, and a positive integer&nbsp;<code>k<\/code>, return&nbsp;<em>the&nbsp;<strong>maximum total value<\/strong>&nbsp;of coins you can have in your wallet if you choose&nbsp;<strong>exactly<\/strong><\/em>&nbsp;<code>k<\/code>&nbsp;<em>coins optimally<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/11\/09\/e1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> piles = [[1,100,3],[7,8,9]], k = 2\n<strong>Output:<\/strong> 101\n<strong>Explanation:<\/strong>\nThe above diagram shows the different ways we can choose k coins.\nThe maximum total we can obtain is 101.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> piles = [[100],[100],[100],[100],[100],[100],[1,1,1,1,1,1,700]], k = 7\n<strong>Output:<\/strong> 706\n<strong>Explanation:\n<\/strong>The maximum total can be obtained if we choose all coins from the last pile.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == piles.length<\/code><\/li><li><code>1 &lt;= n &lt;= 1000<\/code><\/li><li><code>1 &lt;= piles[i][j] &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= k &lt;= sum(piles[i].length) &lt;= 2000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>let dp(i, k) be the maximum value of picking k elements using piles[i:n].<\/p>\n\n\n\n<p>dp(i, k) = max(dp(i + 1, k), sum(piles[i][0~j]) + dp(i + 1, k &#8211; j &#8211; 1)), 0 &lt;= j &lt; len(piles[i])<\/p>\n\n\n\n<p>Time complexity: O(n * m), m = sum(piles[i]) &lt;= 2000<br>Space complexity: O(n * k)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def maxValueOfCoins(self, piles: List[List[int]], k: int) -> int:\n    n = len(piles)\n    \n    @cache\n    def dp(i: int, k: int) -> int:\n      \"\"\"Max value of picking k elements using piles[i:n].\"\"\"\n      if i == n: return 0\n      ans, cur = dp(i + 1, k), 0      \n      for j in range(min(len(piles[i]), k)):        \n        ans = max(ans, (cur := cur + piles[i][j]) + dp(i + 1, k - j - 1))\n      return ans\n    \n    return dp(0, k)\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;piles&nbsp;of coins on a table. Each pile consists of a&nbsp;positive number&nbsp;of coins of assorted denominations. In one move, you can choose any coin on&nbsp;top&nbsp;of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,217],"class_list":["post-9596","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9596"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9596\/revisions"}],"predecessor-version":[{"id":9600,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9596\/revisions\/9600"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}