<?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>profit Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/profit/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/profit/</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>profit Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/profit/</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>
		<item>
		<title>花花酱 LeetCode 309. Best Time to Buy and Sell Stock with Cooldown</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-309-best-time-to-buy-and-sell-stock-with-cooldown/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-309-best-time-to-buy-and-sell-stock-with-cooldown/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 06 Jan 2018 18:57:32 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[profit]]></category>
		<category><![CDATA[stock]]></category>
		<category><![CDATA[time]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1517</guid>

					<description><![CDATA[<p>题目大意：给你每天的股价，没有交易次数限制，但是卖出后要休息一天才能再买进。问你最大收益是多少？ Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-309-best-time-to-buy-and-sell-stock-with-cooldown/">花花酱 LeetCode 309. Best Time to Buy and Sell Stock with Cooldown</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/oL6mRyTn56M?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p>题目大意：给你每天的股价，没有交易次数限制，但是卖出后要休息一天才能再买进。问你最大收益是多少？</p>
<p>Say you have an array for which the <i>i</i><sup>th</sup> element is the price of a given stock on day <i>i</i>.</p>
<p>Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:</p>
<ul>
<li>You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).</li>
<li>After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)</li>
</ul>
<p><b>Example:</b></p><pre class="crayon-plain-tag">prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]</pre><p><strong>Idea:</strong></p>
<p>DP</p>
<p><img class="alignnone size-full wp-image-1522" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/309-ep150.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/309-ep150.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/309-ep150-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/01/309-ep150-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><strong>Solution:</strong></p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 12 ms
class Solution {
public:
    int maxProfit(vector&lt;int&gt;&amp; prices) {
        int sold = 0;
        int rest = 0;
        int hold = INT_MIN;
        for (const int price : prices) {
            int prev_sold = sold;
            sold = hold + price;
            hold = max(hold, rest - price);
            rest = max(rest, prev_sold);
        }
        return max(rest, sold);
    }
};</pre><p><strong>Related Problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-121-best-time-to-buy-and-sell-stock/">花花酱 LeetCode 121. Best Time to Buy and Sell Stock</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-309-best-time-to-buy-and-sell-stock-with-cooldown/">花花酱 LeetCode 309. Best Time to Buy and Sell Stock with Cooldown</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-309-best-time-to-buy-and-sell-stock-with-cooldown/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
