{"id":5915,"date":"2019-12-02T00:09:16","date_gmt":"2019-12-02T08:09:16","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5915"},"modified":"2019-12-03T22:01:41","modified_gmt":"2019-12-04T06:01:41","slug":"leetcode-1278-palindrome-partitioning-iii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1278-palindrome-partitioning-iii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1278. Palindrome Partitioning III"},"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 1278. Palindrome Partitioning III - \u5237\u9898\u627e\u5de5\u4f5c EP280\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/kD6ShM6jr3g?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>You are given a string&nbsp;<code>s<\/code>&nbsp;containing lowercase letters and an integer&nbsp;<code>k<\/code>. You need to :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, change some characters of&nbsp;<code>s<\/code>&nbsp;to other lowercase English letters.<\/li><li>Then divide&nbsp;<code>s<\/code>&nbsp;into&nbsp;<code>k<\/code>&nbsp;non-empty disjoint substrings such that each substring is palindrome.<\/li><\/ul>\n\n\n\n<p>Return the minimal number of characters that you need to change&nbsp;to divide the string.<\/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> s = \"abc\", k = 2\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>&nbsp;You can split the string into \"ab\" and \"c\", and change 1 character in \"ab\" to make it palindrome.\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> s = \"aabbc\", k = 3\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong>&nbsp;You can split the string into \"aa\", \"bb\" and \"c\", all of them are palindrome.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"leetcode\", k = 8\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= k &lt;= s.length &lt;= 100<\/code>.<\/li><li><code>s<\/code>&nbsp;only contains lowercase English letters.<\/li><\/ul>\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\/12\/1278-ep280.png\" alt=\"\" class=\"wp-image-5921\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1278-ep280.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1278-ep280-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1278-ep280-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>dp[i][k] := min changes to make s[0~i] into k palindromes<br>dp[i][k] = min(dp[j][k &#8211; 1] + cost(j + 1, i))  0 &lt;= j &lt; i<\/p>\n\n\n\n<p>ans = dp[n-1][K]<\/p>\n\n\n\n<p>Time complexity: O(n^2 * K) = O(n^3)<br>Space complexity: O(n*n + n*K) = O(n^2)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua, 8 ms, 9.1 MB\nclass Solution {\npublic:\n  int palindromePartition(string s, int K) {\n    const int n = s.length();\n    auto minChange = [&](int i, int j) {\n      int c = 0;\n      while (i < j) \n        if (s[i++] != s[j--]) ++c;      \n      return c;\n    };\n    vector<vector<int>> cost(n, vector<int>(n));\n    for (int i = 0; i < n; ++i)\n      for (int j = i + 1; j < n; ++j)\n        cost[i][j] = minChange(i, j);\n\n    \/\/ dp[i][j] := min changes to make s[0~i] into k palindromes\n    vector<vector<int>> dp(n, vector<int>(K + 1, INT_MAX \/ 2));\n    for (int i = 0; i < n; ++i) {      \n      dp[i][1] = cost[0][i];\n      for (int k = 1; k <= K; ++k)\n        for (int j = 0; j < i; ++j)\n          dp[i][k] = min(dp[i][k], dp[j][k - 1] + cost[j + 1][i]);        \n    }\n    return dp[n - 1][K];\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/DP+DP<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua, 4 ms, 9.1MB\nclass Solution {\npublic:\n  int palindromePartition(string s, int K) {\n    const int n = s.length();    \n    vector<vector<int>> cost(n, vector<int>(n));\n    for (int l = 2; l <= n; ++l)\n      for (int i = 0, j = l - 1; j < n; ++i, ++j)\n        cost[i][j] = (s[i] != s[j]) + cost[i + 1][j - 1];\n    \n    vector<vector<int>> dp(n, vector<int>(K + 1, INT_MAX \/ 2));\n    for (int i = 0; i < n; ++i) {      \n      dp[i][1] = cost[0][i];\n      for (int k = 2; k <= K; ++k)\n        for (int j = 0; j < i; ++j)\n          dp[i][k] = min(dp[i][k], dp[j][k - 1] + cost[j + 1][i]);        \n    }\n    return dp[n - 1][K];\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"Python\">\n\/\/ Author: Huahua\nimport functools\nclass Solution:\n  def palindromePartition(self, s: str, K: int) -> int:\n    @functools.lru_cache(None)\n    def cost(i: int, j: int) -> int:\n      if i >= j: return 0\n      return cost(i + 1, j - 1) + (1 if s[i] != s[j] else 0)\n    \n    @functools.lru_cache(None)\n    def dp(i: int, k: int) -> int:\n      if k == 1: return cost(0, i)\n      if k == i + 1: return 0\n      if k > i + 1: return 1**9\n      return min([dp(j, k - 1) + cost(j + 1, i) for j in range(i)])        \n    \n    return dp(len(s) - 1, K)\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s&nbsp;containing lowercase letters and an integer&nbsp;k. You need to : First, change some characters of&nbsp;s&nbsp;to other lowercase English letters. Then divide&nbsp;s&nbsp;into&nbsp;k&nbsp;non-empty&#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,520,150],"class_list":["post-5915","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","tag-k-groups","tag-partition","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5915","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=5915"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5915\/revisions"}],"predecessor-version":[{"id":5922,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5915\/revisions\/5922"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}