<?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^2*k) Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/on2k/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/on2k/</link>
	<description></description>
	<lastBuildDate>Wed, 05 Feb 2020 06:56:25 +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^2*k) Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/on2k/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1335. Minimum Difficulty of a Job Schedule</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1335-minimum-difficulty-of-a-job-schedule/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1335-minimum-difficulty-of-a-job-schedule/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 27 Jan 2020 09:39:57 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[groups]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[O(n^2*k)]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6152</guid>

					<description><![CDATA[<p>You want to schedule a list of jobs in&#160;d&#160;days. Jobs are dependent (i.e To work on the&#160;i-th&#160;job, you have to finish all the jobs&#160;j&#160;where&#160;0 &#60;=&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1335-minimum-difficulty-of-a-job-schedule/">花花酱 LeetCode 1335. Minimum Difficulty of a Job Schedule</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-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1335. Minimum Difficulty of a Job Schedule - 刷题找工作 EP302" width="500" height="281" src="https://www.youtube.com/embed/eRBpfoWujQM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You want to schedule a list of jobs in&nbsp;<code>d</code>&nbsp;days. Jobs are dependent (i.e To work on the&nbsp;<code>i-th</code>&nbsp;job, you have to finish all the jobs&nbsp;<code>j</code>&nbsp;where&nbsp;<code>0 &lt;= j &lt; i</code>).</p>



<p>You have to finish&nbsp;<strong>at least</strong>&nbsp;one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the&nbsp;<code>d</code>&nbsp;days. The difficulty of a day is the maximum difficulty of a job done in that day.</p>



<p>Given an array of integers&nbsp;<code>jobDifficulty</code>&nbsp;and an integer&nbsp;<code>d</code>. The difficulty of the&nbsp;<code>i-th</code>&nbsp;job is&nbsp;<code>jobDifficulty[i]</code>.</p>



<p>Return&nbsp;<em>the minimum difficulty</em>&nbsp;of a job schedule. If you cannot find a schedule for the jobs return&nbsp;<strong>-1</strong>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2
<strong>Output:</strong> 7
<strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6.
Second day you can finish the last job, total difficulty = 1.
The difficulty of the schedule = 6 + 1 = 7 
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> jobDifficulty = [9,9,9], d = 4
<strong>Output:</strong> -1
<strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> jobDifficulty = [1,1,1], d = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> jobDifficulty = [7,1,7,1,7,1], d = 3
<strong>Output:</strong> 15
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> jobDifficulty = [11,111,22,222,33,333,44,444], d = 6
<strong>Output:</strong> 843
</pre>



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



<ul><li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li><li><code>0 &lt;=&nbsp;jobDifficulty[i] &lt;= 1000</code></li><li><code>1 &lt;= d &lt;= 10</code></li></ul>



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



<figure class="wp-block-image"><img width="1024" height="580" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/01/56F9A2C8-3F83-4C84-880B-F7551A06029D-1024x580.jpeg" alt="" class="wp-image-6157" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/01/56F9A2C8-3F83-4C84-880B-F7551A06029D-1024x580.jpeg 1024w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/01/56F9A2C8-3F83-4C84-880B-F7551A06029D-300x170.jpeg 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/01/56F9A2C8-3F83-4C84-880B-F7551A06029D-768x435.jpeg 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>dp[i][k] := min difficulties to schedule jobs 1~i in k days.</p>



<p>Schedule 1 ~ j in k &#8211; 1 days and schedule j + 1 ~ i in 1 day.</p>



<p>Init: dp[0][0] = 0<br>Transition: dp[i][k] := min(dp[j][k -1] + max(jobs[j + 1 ~ i]), k &#8211; 1 &lt;= j &lt; i<br>Answer: dp[n][d]</p>



<p>Time complexity: O(n^2*d)<br>Space complexity: O(n)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
// Author: Huahua
class Solution {
public:
  int minDifficulty(vector&lt;int&gt;&amp; jobs, int d) {
    const int n = jobs.size();
    if (d &gt; n) return -1;    
    vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(d + 1, INT_MAX / 2));
    
    dp[0][0] = 0;
    for (int i = 1; i &lt;= n; ++i)      
      for (int j = i - 1, md = 0; j &gt;= 0; --j) {
        md = max(md, jobs[j]);
        for (int k = 1; k &lt;= min(i, d); ++k)                    
          dp[i][k] = min(dp[i][k], dp[j][k - 1] + md);
      }
    
    return dp[n][d];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1335-minimum-difficulty-of-a-job-schedule/">花花酱 LeetCode 1335. Minimum Difficulty of a Job Schedule</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-1335-minimum-difficulty-of-a-job-schedule/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
