<?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>assignment Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/assignment/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/assignment/</link>
	<description></description>
	<lastBuildDate>Sat, 19 May 2018 03:31:53 +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>assignment Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/assignment/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 826. Most Profit Assigning Work</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-826-most-profit-assigning-work/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-826-most-profit-assigning-work/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 29 Apr 2018 21:00:03 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[assignment]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[profit]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2795</guid>

					<description><![CDATA[<p>Problem We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job. Now we have some workers. worker[i] is the ability of the ith worker,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-826-most-profit-assigning-work/">花花酱 LeetCode 826. Most Profit Assigning Work</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/hh1hF2hS3C4?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>We have jobs: <code>difficulty[i]</code> is the difficulty of the <code>i</code>th job, and <code>profit[i]</code> is the profit of the <code>i</code>th job.</p>
<p>Now we have some workers. <code>worker[i]</code> is the ability of the <code>i</code>th worker, which means that this worker can only complete a job with difficulty at most <code>worker[i]</code>.</p>
<p>Every worker can be assigned at most one job, but one job can be completed multiple times.</p>
<p>For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0.</p>
<p>What is the most profit we can make?</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
<strong>Output: </strong>100 
<strong>Explanation: W</strong>orkers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately.</pre>
<p><strong>Notes:</strong></p>
<ul>
<li><code>1 &lt;= difficulty.length = profit.length &lt;= 10000</code></li>
<li><code>1 &lt;= worker.length &lt;= 10000</code></li>
<li><code>difficulty[i], profit[i], worker[i]</code>  are in range <code>[1, 10^5]</code></li>
</ul>
<h1><strong>Solution 1: Sorting + Two pointers</strong></h1>
<p>Time complexity: O(nlogn + mlogm)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 90 ms
class Solution {
public:
  int maxProfitAssignment(vector&lt;int&gt;&amp; difficulty, vector&lt;int&gt;&amp; profit, vector&lt;int&gt;&amp; worker) {
    const int n = difficulty.size();
    vector&lt;pair&lt;int, int&gt;&gt; jobs; // difficulty, profit
    
    for (int i = 0; i &lt; n; ++i)
      jobs.emplace_back(difficulty[i], profit[i]);
    
    std::sort(jobs.begin(), jobs.end());
    std::sort(worker.begin(), worker.end());
    
    int ans = 0;
    int i = 0;
    int best = 0;
    for (int level : worker) {
      while (i &lt; n &amp;&amp; level &gt;= jobs[i].first)
        best = max(best, jobs[i++].second);
      ans += best;
    }
    return ans;
  }
};</pre><p></p>
<h1>Solution 2: Bucket + Greedy</h1>
<p>Key idea: for each difficulty D, find the most profit job whose requirement is &lt;= D.</p>
<p>Three steps:</p>
<ol>
<li>for each difficulty D, find the most profit job whose requirement is == D, best[D] = max{profit of difficulty D}.</li>
<li>if difficulty D &#8211; 1 can make more profit than difficulty D, best[D] = max(best[D], best[D &#8211; 1]).</li>
<li>The max profit each worker at skill level D can make is best[D].</li>
</ol>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(10000)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 112 ms
class Solution {
public:
  int maxProfitAssignment(vector&lt;int&gt;&amp; difficulty, vector&lt;int&gt;&amp; profit, vector&lt;int&gt;&amp; worker) {
    const int N = 100000;
    // max profit at difficulty i
    vector&lt;int&gt; max_profit(N + 1, 0);
    for (int i = 0; i &lt; difficulty.size(); ++i)
      max_profit[difficulty[i]] = max(max_profit[difficulty[i]], profit[i]);
    for (int i = 2; i &lt;= N; ++i)
      max_profit[i] = max(max_profit[i], max_profit[i - 1]);
    int ans = 0;
    for (int level : worker)
      ans += max_profit[level];
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>C++ using map</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 112 ms
class Solution {
public:
  int maxProfitAssignment(vector&lt;int&gt;&amp; difficulty, vector&lt;int&gt;&amp; profit, vector&lt;int&gt;&amp; worker) {
    map&lt;int, int&gt; bests;
    for (int i = 0; i &lt; difficulty.size(); ++i)
      bests[difficulty[i]] = max(bests[difficulty[i]], profit[i]);
    int best = 0;
    for (auto it = bests.begin(); it != bests.end(); ++it)
      it-&gt;second = best = max(it-&gt;second, best);    
    int ans = 0;
    for (int level : worker)
      ans += prev(bests.upper_bound(level))-&gt;second;
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-826-most-profit-assigning-work/">花花酱 LeetCode 826. Most Profit Assigning Work</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/greedy/leetcode-826-most-profit-assigning-work/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
