{"id":782,"date":"2020-03-04T22:02:47","date_gmt":"2020-03-05T06:02:47","guid":{"rendered":"http:\/\/209.126.2.187\/?p=782"},"modified":"2020-05-05T22:43:57","modified_gmt":"2020-05-06T05:43:57","slug":"dp-in-intervals-dynamic","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2020\/03\/04\/dp-in-intervals-dynamic\/","title":{"rendered":"DP in Intervals (Dynamic)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>Some problems in this category has something dynamic. Like several elements merge into one element or eliminate one element with the a cost related to its neighbors. The key point of these problems is think backward: think about how the last element is formed.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Details<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Minimum Cost to Merge Stones I<\/h4>\n\n\n\n<p>There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists of merging 2 consecutive piles into one pile, and the cost of this move is equal to the total number of stones in these 2 piles. Find the minimum cost to merge all piles of stones into one pile.<\/p>\n\n\n\n<p>Think backwards. Consider the last pile in range <code>nums[i,...,j]<\/code> is formed by &#8220;Super Stone&#8221; <code>nums[i,...,k]<\/code> and &#8220;Super Stone&#8221; <code>nums[k + 1,...,j]<\/code>. Here &#8220;Super Stone&#8221; <code>nums[i,...,k]<\/code> means these stones will form into a single stone. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">alg[i][j]=min(alg[i][j],alg[i][k]+alg[k+1][j]+sum[i][j]), k in [i, j - 1]<\/code><\/pre>\n\n\n\n<p>An interesting variance of this problem is that any two stones can be merged. In this case, we can greedily choose smaller stones to merge together, which becomes a Huffman Code problem. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><a rel=\"noreferrer noopener\" href=\"https:\/\/leetcode.com\/problems\/minimum-cost-to-merge-stones\/\" target=\"_blank\">LC 1000. Minimum Cost to Merge Stones II<\/a> <\/h4>\n\n\n\n<p>This problem involves another dimension and it also needs to be thought backwards. Consider the last pile in range <code>nums[i,...,j]<\/code> is formed by <code>K<\/code> piles of stones. <code>nums[i,...,l]<\/code> forms one pile and <code>nums[l + 1,...,j]<\/code> forms <code>K-1<\/code> piles. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">alg[i][j][k] (k >= 2) =\n  min{ alg[i][l][1] + alg[l + 1][j][k - 1] for l in [i, ... j - 1]}\n\/\/ Note that is not  min{ alg[i][l][1] + alg[l + 1][j][k - 1] + sum[i][j]}! Since forming k piles does not need to pay. \nalg[i][j][1] = alg[i][j][k] + sum[i][j]\n\n\/\/ alg[i][i][1] = 0\n\/\/ sum[i][j]: prefix_sum[j] - ((i - 1 >= 0) ? prefix_sum[i - 1] : 0)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.com\/problems\/minimum-cost-tree-from-leaf-values\/\">LC 1130. Minimum Cost Tree From Leaf Values<\/a><\/h4>\n\n\n\n<p>Think backwards. Consider <code>nums[k]<\/code> is the last number in range <code>nums[i,...,j]<\/code>. <code>nums[i,...,k]<\/code> forms the left subtree, and <code>nums[k + 1,...,j]<\/code> forms the right subtree. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">max_num[i][j] = max(max_num[i + 1][j - 1], nums[i], nums[j])\n\nalg[i][j] = min {alg[i][k] + alg[k + 1][j] + max_num[i][k] * max_num[k + 1][j], for k in [i, ..., j - 1]}\n\n\/\/ alg[i][i] = 0\n\/\/ alg[i][i + 1] = nums[i] * nums[i + 1]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.com\/problems\/minimum-cost-tree-from-leaf-values\/\">LC 312.\u00a0Burst Balloons<\/a><\/h4>\n\n\n\n<p>Think backwards. Consider <code>nums[k]<\/code> is the last ballon in range <code>nums[i+1,...,j-1]<\/code>. Then<code>nums[i+1,...,k-1]<\/code> should be bursted out, and <code>nums[k+1,...,j-1]<\/code> should also be bursted out. <code>nums[i+1,...,k-1]<\/code> will be <code>alg[i][k]<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">\/\/ pad nums => {1, nums[0], ..., nums[n - 1], 1}\nalg[i][j] = max { nums[i] * nums[j] * nums[k] + alg[i][k] + alg[k][j], for k in [i + 1, ..., j - 1]}\n\n\/\/ alg[i][i] = INT_MIN\n\/\/ alg[i][i + 1] = INT_MIN<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary Some problems in this category has something dynamic. Like several elements merge into one element or eliminate one element with the a cost related to its neighbors. The key point of these problems is think backward: think about how the last element is formed. Details Minimum Cost to Merge Stones I There are N&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,50],"tags":[],"class_list":["post-782","post","type-post","status-publish","format-standard","hentry","category-alg","category-dp-in-intervals"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=782"}],"version-history":[{"count":10,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/782\/revisions"}],"predecessor-version":[{"id":1165,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/782\/revisions\/1165"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}