{"id":5183,"date":"2019-05-14T20:33:31","date_gmt":"2019-05-15T03:33:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5183"},"modified":"2019-05-14T22:45:47","modified_gmt":"2019-05-15T05:45:47","slug":"leetcode-1043-partition-array-for-maximum-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1043-partition-array-for-maximum-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1043. Partition Array for Maximum Sum"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1043. Partition Array for Maximum Sum - \u5237\u9898\u627e\u5de5\u4f5c EP250\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/3M8q-wB2tmw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given an integer array&nbsp;<code>A<\/code>, you partition the array into (contiguous) subarrays of length at most&nbsp;<code>K<\/code>.&nbsp; After partitioning, each subarray has their values changed to become the maximum value of that subarray.<\/p>\n\n\n\n<p>Return the largest sum of the given array after partitioning.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input: <\/strong>A = [1,15,7,9,2,5,10], K = 3\n<strong>Output: <\/strong>84\n<strong>Explanation<\/strong>: A becomes [15,15,15,9,10,10,10]<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= K &lt;= A.length&nbsp;&lt;= 500<\/code><\/li><li><code>0 &lt;= A[i] &lt;= 10^6<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/05\/1043-ep250.png\" alt=\"\" class=\"wp-image-5190\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/05\/1043-ep250.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/05\/1043-ep250-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/05\/1043-ep250-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Time complexity: O(n*k)<br>Space complexity: O(n)<\/p>\n\n\n\n<p>dp[i] := max sum of A[1] ~ A[i]<br>init: dp[0] = 0<br>transition: dp[i] = max{dp[i &#8211; k] + max(A[i-k:i]) * k}, 1 &lt;= k &lt;= min(i, K)<br>ans: dp[n]<\/p>\n\n\n\n<pre class=\"crayon:false\">A = | 2 | 1 | 4 | 3 |\nK = 3\ndp[0] = 0\ndp[1] = max(dp[0] + 2 * 1) = 2\ndp[2] = max(dp[0] + 2 * 2, dp[1] + 1 * 1) = max(4, 3) = 4\ndp[3] = max(dp[0] + 4 * 3, dp[1] + 4 * 2, dp[2] + 4 * 1) = max(12, 10, 8) = 12\ndp[4] = max(dp[1] + 4 * 3, dp[2] + 4 * 2, dp[3] + 3 * 1) = max(14, 12, 15) = 15\nbest = | 4 | 4 | 4 | 3 |\n<\/pre>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int maxSumAfterPartitioning(vector<int>& A, int K) {\n    int n = A.size();\n    vector<int> dp(n + 1, 0);\n    for (int i = 1; i <= n; ++i) {      \n      int m = INT_MIN;\n      for (int k = 1; k <= K &#038;&#038; i - k >= 0; ++k) {\n        m = max(m, A[i - k]);\n        dp[i] = max(dp[i], dp[i - k] + m * k);\n      }\n    }\n    return dp[n];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;A, you partition the array into (contiguous) subarrays of length at most&nbsp;K.&nbsp; After partitioning, each subarray has their values changed to become&#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,177],"class_list":["post-5183","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5183","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=5183"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5183\/revisions"}],"predecessor-version":[{"id":5191,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5183\/revisions\/5191"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}