<?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>all subsets Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/all-subsets/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/all-subsets/</link>
	<description></description>
	<lastBuildDate>Mon, 11 Jan 2021 03:08:08 +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>all subsets Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/all-subsets/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1723. Find Minimum Time to Finish All Jobs</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1723-find-minimum-time-to-finish-all-jobs/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1723-find-minimum-time-to-finish-all-jobs/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Jan 2021 19:30:53 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[all subsets]]></category>
		<category><![CDATA[bitmask]]></category>
		<category><![CDATA[dp]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7975</guid>

					<description><![CDATA[<p>You are given an integer array&#160;jobs, where&#160;jobs[i]&#160;is the amount of time it takes to complete the&#160;ith&#160;job. There are&#160;k&#160;workers that you can assign jobs to. Each&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1723-find-minimum-time-to-finish-all-jobs/">花花酱 LeetCode 1723. Find Minimum Time to Finish All Jobs</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>You are given an integer array&nbsp;<code>jobs</code>, where&nbsp;<code>jobs[i]</code>&nbsp;is the amount of time it takes to complete the&nbsp;<code>i<sup>th</sup></code>&nbsp;job.</p>



<p>There are&nbsp;<code>k</code>&nbsp;workers that you can assign jobs to. Each job should be assigned to&nbsp;<strong>exactly</strong>&nbsp;one worker. The&nbsp;<strong>working time</strong>&nbsp;of a worker is the sum of the time it takes to complete all jobs assigned to them. Your goal is to devise an optimal assignment such that the&nbsp;<strong>maximum working time</strong>&nbsp;of any worker is&nbsp;<strong>minimized</strong>.</p>



<p><em>Return the&nbsp;<strong>minimum</strong>&nbsp;possible&nbsp;<strong>maximum working time</strong>&nbsp;of any assignment.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> jobs = [3,2,3], k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> By assigning each person one job, the maximum time is 3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> jobs = [1,2,4,7,8], k = 2
<strong>Output:</strong> 11
<strong>Explanation:</strong> Assign the jobs the following way:
Worker 1: 1, 2, 8 (working time = 1 + 2 + 8 = 11)
Worker 2: 4, 7 (working time = 4 + 7 = 11)
The maximum working time is 11.</pre>



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



<ul><li><code>1 &lt;= k &lt;= jobs.length &lt;= 12</code></li><li><code>1 &lt;= jobs[i] &lt;= 10<sup>7</sup></code></li></ul>



<h2><strong>Solution 1: All subsets</strong></h2>



<p>dp[i][t] := min of max working time by assigning a subset of jobs s to the first i workers.</p>



<p>dp[i][t] = min{max(dp[i &#8211; 1][s], cost[s ^ t])} where s is a subset of t.</p>



<p>Time complexity: O(k*3^n)<br>Space complexity: O(k*2^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 minimumTimeRequired(vector&lt;int&gt;&amp; jobs, int k) {    
    const int n = jobs.size();
    int dp[13][4096];
    memset(dp, 0x7f, sizeof(dp));
    dp[0][0] = 0;
    
    vector&lt;int&gt; cost(1 &lt;&lt; n);
    for (int t = 0; t &lt; 1 &lt;&lt; n; ++t) {
      for (int j = 0; j &lt; n; ++j)
        if (t &gt;&gt; j &amp; 1) cost[t] += jobs[j];
      dp[1][t] = cost[t]; // do jobs t by one worker
    }
    
    for (int i = 2; i &lt;= k; ++i)
      for (int t = 0; t &lt; 1 &lt;&lt; n; ++t)
        for (int s = t; s; s = (s - 1) &amp; t)
          dp[i][t] = min(dp[i][t], max(dp[i - 1][s], cost[t ^ s]));
    return dp[k][(1 &lt;&lt; n) - 1];
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Search + Pruning</strong></h2>



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  int minimumTimeRequired(vector&lt;int&gt;&amp; jobs, int k) {
    vector&lt;int&gt; times(k);
    int ans = INT_MAX;
    function&lt;void(int, int)&gt; dfs = [&amp;](int i, int cur) {
      if (cur &gt;= ans) return;
      if (i == jobs.size()) {
        ans = cur;
        return;
      }
      unordered_set&lt;int&gt; seen;
      for (int j = 0; j &lt; k; ++j) {
        if (!seen.insert(times[j]).second) continue;
        times[j] += jobs[i];
        dfs(i + 1, max(cur, times[j]));
        times[j] -= jobs[i];
      }
    };
    sort(rbegin(jobs), rend(jobs));
    dfs(0, 0);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1723-find-minimum-time-to-finish-all-jobs/">花花酱 LeetCode 1723. Find Minimum Time to Finish All Jobs</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-1723-find-minimum-time-to-finish-all-jobs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
