<?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>O(n^3*k) Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/on3k/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/on3k/</link>
	<description></description>
	<lastBuildDate>Sun, 17 May 2020 04:03:05 +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>O(n^3*k) Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/on3k/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1444. Number of Ways of Cutting a Pizza</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1444-number-of-ways-of-cutting-a-pizza/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1444-number-of-ways-of-cutting-a-pizza/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 16 May 2020 00:17:48 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[O(n^3*k)]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6750</guid>

					<description><![CDATA[<p>Given a rectangular pizza represented as a&#160;rows x cols&#160;matrix containing the following characters:&#160;'A'&#160;(an apple) and&#160;'.'&#160;(empty cell) and given the integer&#160;k. You have to cut the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1444-number-of-ways-of-cutting-a-pizza/">花花酱 LeetCode 1444. Number of Ways of Cutting a Pizza</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 1444. Number of Ways of Cutting a Pizza - 刷题找工作 EP326" width="500" height="375" src="https://www.youtube.com/embed/q2Wh5v___r8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given a rectangular pizza represented as a&nbsp;<code>rows x cols</code>&nbsp;matrix containing the following characters:&nbsp;<code>'A'</code>&nbsp;(an apple) and&nbsp;<code>'.'</code>&nbsp;(empty cell) and given the integer&nbsp;<code>k</code>. You have to cut the pizza into&nbsp;<code>k</code>&nbsp;pieces using&nbsp;<code>k-1</code>&nbsp;cuts.&nbsp;</p>



<p>For each cut you choose the direction: vertical or horizontal, then you choose a cut position at the cell boundary and cut the pizza into two pieces. If you cut the pizza vertically, give the left part of the pizza to a person. If you cut the pizza horizontally, give the upper part of the pizza to a person. Give the last piece of pizza to the last person.</p>



<p><em>Return the number of ways of cutting the pizza such that each piece contains&nbsp;<strong>at least</strong>&nbsp;one apple.&nbsp;</em>Since the answer can be a huge number, return this modulo 10^9 + 7.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/04/23/ways_to_cut_apple_1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> pizza = ["A..","AAA","..."], k = 3
<strong>Output:</strong> 3 
<strong>Explanation:</strong> The figure above shows the three ways to cut the pizza. Note that pieces must contain at least one apple.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> pizza = ["A..","AA.","..."], k = 3
<strong>Output:</strong> 1
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> pizza = ["A..","A..","..."], k = 1
<strong>Output:</strong> 1
</pre>



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



<ul><li><code>1 &lt;= rows, cols &lt;= 50</code></li><li><code>rows ==&nbsp;pizza.length</code></li><li><code>cols ==&nbsp;pizza[i].length</code></li><li><code>1 &lt;= k &lt;= 10</code></li><li><code>pizza</code>&nbsp;consists of characters&nbsp;<code>'A'</code>&nbsp;and&nbsp;<code>'.'</code>&nbsp;only.</li></ul>



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



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/05/1444-ep326.png" alt="" class="wp-image-6755" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/05/1444-ep326.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/05/1444-ep326-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/05/1444-ep326-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/05/1444-ep326-2.png" alt="" class="wp-image-6756" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/05/1444-ep326-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/05/1444-ep326-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/05/1444-ep326-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/05/1444-ep326-3.png" alt="" class="wp-image-6757" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/05/1444-ep326-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/05/1444-ep326-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/05/1444-ep326-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>dp(n, m, k) := # of ways to cut pizza[n:N][m:M] with k cuts.</p>



<p>dp(n, m, k) = sum(hasApples(n, m, N &#8211; 1, y) * dp(y + 1, n, k &#8211; 1) for y in range(n, M)) + sum(hasApples(n, m, x, M &#8211; 1) * dp(m, x + 1, k &#8211; 1) for x in range(n, M))</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int ways(vector&lt;string&gt;&amp; pizza, int K) {
    constexpr int kMod = 1e9 + 7;
    const int M = pizza.size();
    const int N = pizza[0].size();
    
    vector&lt;vector&lt;int&gt;&gt; A(M + 1, vector&lt;int&gt;(N + 1));  
    for (int y = 0; y &lt; M; ++y)
      for (int x = 0; x &lt; N; ++x)
        A[y + 1][x + 1] = A[y + 1][x] + A[y][x + 1] + (pizza[y][x] == 'A') - A[y][x];
    
    auto hasApples = [&amp;](int x1, int y1, int x2, int y2) {
      return (A[y2 + 1][x2 + 1] - A[y2 + 1][x1] - A[y1][x2 + 1] + A[y1][x1]) &gt; 0;
    };
    
    vector&lt;vector&lt;vector&lt;int&gt;&gt;&gt; cache(M, vector&lt;vector&lt;int&gt;&gt;(N, vector&lt;int&gt;(K, -1)));
    
    // dp(m, n, k) := # of ways to cut pizza[m:M][n:N] with k cuts.
    function&lt;int(int, int, int)&gt; dp = [&amp;](int m, int n, int k) -&gt; int {
      if (k == 0) return hasApples(n, m, N - 1, M - 1);
      int&amp; ans = cache[m][n][k];
      if (ans != -1) return ans;      
      ans = 0;      
      for (int y = m; y &lt; M - 1; ++y)  // Cut horizontally.
        ans = (ans + hasApples(n, m, N - 1, y) * dp(y + 1, n, k - 1)) % kMod;         
      for (int x = n; x &lt; N - 1; ++x)  // Cut vertically.
        ans = (ans + hasApples(n, m, x, M - 1) * dp(m, x + 1, k - 1)) % kMod;
      return ans;
    };  
    return dp(0, 0, K - 1);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1444-number-of-ways-of-cutting-a-pizza/">花花酱 LeetCode 1444. Number of Ways of Cutting a Pizza</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-1444-number-of-ways-of-cutting-a-pizza/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
