<?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>groups Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/groups/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/groups/</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>groups Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/groups/</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>
		<item>
		<title>花花酱 LeetCode 410. Split Array Largest Sum</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-410-split-array-largest-sum/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-410-split-array-largest-sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 04 Jul 2018 04:49:04 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[groups]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[largest]]></category>
		<category><![CDATA[subarray]]></category>
		<category><![CDATA[sum]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2986</guid>

					<description><![CDATA[<p>Problem Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-410-split-array-largest-sum/">花花酱 LeetCode 410. Split Array Largest Sum</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/_k-Jb4b7b_0?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>Given an array which consists of non-negative integers and an integer <i>m</i>, you can split the array into <i>m</i> non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these <i>m</i> subarrays.</p>
<p><b>Note:</b><br />
If <i>n</i> is the length of array, assume the following constraints are satisfied:</p>
<ul>
<li>1 ≤ <i>n</i> ≤ 1000</li>
<li>1 ≤ <i>m</i> ≤ min(50, <i>n</i>)</li>
</ul>
<p><b>Examples:</b></p>
<pre class="crayon:false">Input:
<b>nums</b> = [7,2,5,10,8]
<b>m</b> = 2

Output:
18

Explanation:
There are four ways to split <b>nums</b> into two subarrays.
The best way is to split it into <b>[7,2,5]</b> and <b>[10,8]</b>,
where the largest sum among the two subarrays is only 18.
</pre>
<h1> <img class="alignnone size-full wp-image-2997" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></h1>
<h1><img class="alignnone size-full wp-image-2996" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></h1>
<h1><img class="alignnone size-full wp-image-2999" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-3.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/07/410-ep203-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></h1>
<h1><strong>Solution: DP</strong></h1>
<p>Time complexity: O(n^2*m)</p>
<p>Space complexity: O(n*m)</p>
<p>C++ / Recursion + Memorization</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 111 ms
class Solution {
public:
  int splitArray(vector&lt;int&gt;&amp; nums, int m) {
    const int n = nums.size();
    sums_ = vector&lt;int&gt;(n);
    mem_ = vector&lt;vector&lt;int&gt;&gt;(n, vector&lt;int&gt;(m + 1, INT_MAX));
    sums_[0] = nums[0];
    for (int i = 1; i &lt; n; ++i)
      sums_[i] = sums_[i - 1] + nums[i];
    return splitArray(nums, n - 1, m);
  }
private:
  vector&lt;vector&lt;int&gt;&gt; mem_;
  vector&lt;int&gt; sums_;
  
  // min of largest sum of spliting nums[0] ~ nums[k] into m groups
  int splitArray(const vector&lt;int&gt;&amp; nums, int k, int m) {
    if (m == 1) return sums_[k];
    if (m &gt; k + 1) return INT_MAX;    
    if (mem_[k][m] != INT_MAX) return mem_[k][m];
    int ans = INT_MAX;
    for (int i = 0; i &lt; k; ++i)
      ans = min(ans, max(splitArray(nums, i, m - 1), sums_[k] - sums_[i]));    
    return mem_[k][m] = ans;
  }
};</pre><p>C++ / DP</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 90 ms
class Solution {
public:
  int splitArray(vector&lt;int&gt;&amp; nums, int m) {
    const int n = nums.size();
    vector&lt;int&gt; sums(n);
    // dp[i][j] := min of largest sum of splitting nums[0] ~ nums[j] into i groups.
    vector&lt;vector&lt;int&gt;&gt; dp(m + 1, vector&lt;int&gt;(n, INT_MAX));
    sums[0] = nums[0];
    for (int i = 1; i &lt; n; ++i)
      sums[i] = sums[i - 1] + nums[i];
    for (int i = 0; i &lt; n; ++i)
      dp[1][i] = sums[i];
    
    for (int i = 2; i &lt;= m; ++i)
      for (int j = i - 1; j &lt; n; ++j)
        for (int k = 0; k &lt; j; ++k)
          dp[i][j] = min(dp[i][j], max(dp[i - 1][k], sums[j] - sums[k]));
    return dp[m][n - 1];
  }  
};</pre><p></p>
<h1><strong>Solution: Binary Search</strong></h1>
<p>Time complexity: O(log(sum(nums))*n)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 3 ms
class Solution {
public:
  int splitArray(vector&lt;int&gt;&amp; nums, int m) {
    long l = *max_element(begin(nums), end(nums));
    long r = accumulate(begin(nums), end(nums), 0L) + 1;
    while (l &lt; r) {
      long limit = (r - l) / 2 + l;
      if (min_groups(nums, limit) &gt; m) 
        l = limit + 1;
      else
        r = limit;
    }
    return l;
  }
private:
  int min_groups(const vector&lt;int&gt;&amp; nums, long limit) {
    long sum = 0;
    int groups = 1;
    for (int num : nums) {
      if (sum + num &gt; limit) {
        sum = num;
        ++groups;
      } else {
        sum += num;
      }
    }    
    return groups;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-410-split-array-largest-sum/">花花酱 LeetCode 410. Split Array Largest Sum</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-410-split-array-largest-sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
