<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>k groups Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/k-groups/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/k-groups/</link>
	<description></description>
	<lastBuildDate>Wed, 04 Dec 2019 06:01:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>k groups Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/k-groups/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1278. Palindrome Partitioning III</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1278-palindrome-partitioning-iii/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1278-palindrome-partitioning-iii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 02 Dec 2019 08:09:16 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[k groups]]></category>
		<category><![CDATA[partition]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5915</guid>

					<description><![CDATA[<p>You are given a string&#160;s&#160;containing lowercase letters and an integer&#160;k. You need to : First, change some characters of&#160;s&#160;to other lowercase English letters. Then divide&#160;s&#160;into&#160;k&#160;non-empty&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1278-palindrome-partitioning-iii/">花花酱 LeetCode 1278. Palindrome Partitioning III</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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">
<iframe title="花花酱 LeetCode 1278. Palindrome Partitioning III - 刷题找工作 EP280" width="500" height="375" src="https://www.youtube.com/embed/kD6ShM6jr3g?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<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>



<ul><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>



<p>Return the minimal number of characters that you need to change&nbsp;to divide the string.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "abc", k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong>&nbsp;You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "aabbc", k = 3
<strong>Output:</strong> 0
<strong>Explanation:</strong>&nbsp;You can split the string into "aa", "bb" and "c", all of them are palindrome.</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "leetcode", k = 8
<strong>Output:</strong> 0
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= k &lt;= s.length &lt;= 100</code>.</li><li><code>s</code>&nbsp;only contains lowercase English letters.</li></ul>



<h2><strong>Solution: DP</strong></h2>



<figure class="wp-block-image"><img 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="(max-width: 960px) 100vw, 960px" /></figure>



<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>



<p>ans = dp[n-1][K]</p>



<p>Time complexity: O(n^2 * K) = O(n^3)<br>Space complexity: O(n*n + n*K) = O(n^2)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 8 ms, 9.1 MB
class Solution {
public:
  int palindromePartition(string s, int K) {
    const int n = s.length();
    auto minChange = [&amp;](int i, int j) {
      int c = 0;
      while (i &lt; j) 
        if (s[i++] != s[j--]) ++c;      
      return c;
    };
    vector&lt;vector&lt;int&gt;&gt; cost(n, vector&lt;int&gt;(n));
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        cost[i][j] = minChange(i, j);

    // dp[i][j] := min changes to make s[0~i] into k palindromes
    vector&lt;vector&lt;int&gt;&gt; dp(n, vector&lt;int&gt;(K + 1, INT_MAX / 2));
    for (int i = 0; i &lt; n; ++i) {      
      dp[i][1] = cost[0][i];
      for (int k = 1; k &lt;= K; ++k)
        for (int j = 0; j &lt; i; ++j)
          dp[i][k] = min(dp[i][k], dp[j][k - 1] + cost[j + 1][i]);        
    }
    return dp[n - 1][K];
  }
};</pre>
</div></div>



<div class="responsive-tabs">
<h2 class="tabtitle">C++/DP+DP</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 4 ms, 9.1MB
class Solution {
public:
  int palindromePartition(string s, int K) {
    const int n = s.length();    
    vector&lt;vector&lt;int&gt;&gt; cost(n, vector&lt;int&gt;(n));
    for (int l = 2; l &lt;= n; ++l)
      for (int i = 0, j = l - 1; j &lt; n; ++i, ++j)
        cost[i][j] = (s[i] != s[j]) + cost[i + 1][j - 1];
    
    vector&lt;vector&lt;int&gt;&gt; dp(n, vector&lt;int&gt;(K + 1, INT_MAX / 2));
    for (int i = 0; i &lt; n; ++i) {      
      dp[i][1] = cost[0][i];
      for (int k = 2; k &lt;= K; ++k)
        for (int j = 0; j &lt; i; ++j)
          dp[i][k] = min(dp[i][k], dp[j][k - 1] + cost[j + 1][i]);        
    }
    return dp[n - 1][K];
  }
};</pre>
</div></div>



<div class="responsive-tabs">
<h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
import functools
class Solution:
  def palindromePartition(self, s: str, K: int) -&gt; int:
    @functools.lru_cache(None)
    def cost(i: int, j: int) -&gt; int:
      if i &gt;= j: return 0
      return cost(i + 1, j - 1) + (1 if s[i] != s[j] else 0)
    
    @functools.lru_cache(None)
    def dp(i: int, k: int) -&gt; int:
      if k == 1: return cost(0, i)
      if k == i + 1: return 0
      if k &gt; i + 1: return 1**9
      return min([dp(j, k - 1) + cost(j + 1, i) for j in range(i)])        
    
    return dp(len(s) - 1, K)</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1278-palindrome-partitioning-iii/">花花酱 LeetCode 1278. Palindrome Partitioning III</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1278-palindrome-partitioning-iii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
