<?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>max subarray Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/max-subarray/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/max-subarray/</link>
	<description></description>
	<lastBuildDate>Mon, 08 Jan 2018 17:00:44 +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>max subarray Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/max-subarray/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 121. Best Time to Buy and Sell Stock</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-121-best-time-to-buy-and-sell-stock/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-121-best-time-to-buy-and-sell-stock/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 23 Dec 2017 01:32:19 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[Easy]]></category>
		<category><![CDATA[max subarray]]></category>
		<category><![CDATA[reduction]]></category>
		<category><![CDATA[stock]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1319</guid>

					<description><![CDATA[<p>题目大意: 给你一只股票每天的价格，如果只能做一次交易（一次买进一次卖出）问你最多能赚多少钱。 Problem: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted&#8230;</p>
<p>The post <a rel="nofollow" href="https://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> 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/8pVhUpF1INw?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p>题目大意: 给你一只股票每天的价格，如果只能做一次交易（一次买进一次卖出）问你最多能赚多少钱。</p>
<p><strong>Problem:</strong></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>If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: [7, 1, 5, 3, 6, 4]
Output: 5

max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: [7, 6, 4, 3, 1]
Output: 0

In this case, no transaction is done, i.e. max profit = 0.</pre><p>&nbsp;</p>
<p><img class="alignnone size-full wp-image-1561" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/121-ep140-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/121-ep140-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/121-ep140-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/121-ep140-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><strong>Idea:</strong></p>
<p>DP</p>
<p><strong>Solution 1:</strong></p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 6 ms
class Solution {
public:
    int maxProfit(vector&lt;int&gt;&amp; prices) {
        const int n = prices.size();
        if (n &lt; 1) return 0;
        vector&lt;int&gt; min_prices(n);
        vector&lt;int&gt; max_profit(n);
        min_prices[0] = prices[0];
        max_profit[0] = 0;
        for (int i = 1; i &lt; n; ++i) {
            min_prices[i] = min(min_prices[i - 1], 
                                prices[i]);
            
            max_profit[i] = max(max_profit[i - 1], 
                                prices[i] - min_prices[i - 1]);
        }
        return max_profit[n - 1];
    }
};</pre><p>C++ / reduce to maximum subarray</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 6 ms
class Solution {
public:
    int maxProfit(vector&lt;int&gt;&amp; prices) {
        int n = prices.size();
        if (n &lt; 2) return 0;
        vector&lt;int&gt; gains(n - 1, 0);
        for (int i = 1; i &lt; n; ++i)
            gains[i - 1] = prices[i] - prices[i - 1];
        return max(0, maxSubArray(gains));
    }
private:
    // From LC 53. Maximum Subarray
    int maxSubArray(vector&lt;int&gt;&amp; nums) {
        vector&lt;int&gt; f(nums.size());
        f[0] = nums[0];
        
        for (int i = 1; i &lt; nums.size(); ++i)
            f[i] = max(f[i - 1] + nums[i], nums[i]);
        
        return *std::max_element(f.begin(), f.end());
    }
};</pre><p><strong>Related Problems:</strong></p>
<ul>
<li><a href="http://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></li>
</ul>
<p>The post <a rel="nofollow" href="https://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> 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-121-best-time-to-buy-and-sell-stock/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
