<?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>stock problems Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/stock-problems/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/stock-problems/</link>
	<description></description>
	<lastBuildDate>Fri, 10 Dec 2021 05:30:10 +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>stock problems Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/stock-problems/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode Ultimate DP Study Plan Day 8</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-8/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-8/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 10 Dec 2021 05:16:04 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[coding with me]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[stock problems]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9073</guid>

					<description><![CDATA[<p>309. Best Time to Buy and Sell Stock with Cooldown [crayon-663c8b99ee5e5918784439/] 714 Best Time to Buy and Sell Stock with Transaction Fee [crayon-663c8b99ee5ea337962987/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-8/">花花酱 LeetCode Ultimate DP Study Plan Day 8</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 is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="LeetCode DP终极学习计划！Day8 | Best Time to Buy and Sell Stock with Cooldown【跟我一起写代码】" width="500" height="281" src="https://www.youtube.com/embed/xXIs_bF8lHE?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<h2><strong>309. Best Time to Buy and Sell Stock with Cooldown</strong></h2>



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

<pre class="crayon-plain-tag"># Author: Huahua
# Time complexity: O(n)
# Space complexity: O(n)
class Solution:
  def maxProfit(self, prices: List[int]) -&gt; int:
    
    @cache
    def dp(i: int) -&gt; Tuple[int, int, int]:
      &quot;&quot;&quot;
      Returns
      1) sold: Max profit w/o holdings. Can't buy. Can't sell.
      2) holding: Max profit w/ holdings. Can't buy, Can sell.
      3) cooldown: Max profit w/o holdings. Can buy. Can't sell.&quot;&quot;&quot;
      if i &lt; 0: return -10**9, -10**9, 0
      sold, holding, cooldown = dp(i - 1)
      
      return (holding + prices[i], # sold
              max(holding, cooldown - prices[i]), # do nothing, or buy.
              max(cooldown, sold)) # cooldown
    
    return max(dp(len(prices) - 1))</pre>
</div></div>



<h2><strong>714 Best Time to Buy and Sell Stock with Transaction Fee</strong></h2>



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

<pre class="crayon-plain-tag"># Author: Huahua
# Time complexity: O(n)
# Space complexity: O(n)
class Solution:
  def maxProfit(self, prices: List[int], fee: int) -&gt; int:
    
    @cache
    def dp(i: int) -&gt; Tuple[int, int]:
      &quot;&quot;&quot;
      Returns:
      1) sold: Max profit w/o holdings.
      2) holding: Max profit w/ holdings.&quot;&quot;&quot;
      if i &lt; 0: return 0, -10**9
      sold, holding = dp(i - 1)
      return (max(sold, holding + prices[i] - fee), # do nothing, sell
              max(holding, sold - prices[i]))       # do nothing, buy
    
    return dp(len(prices) - 1)[0]</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-8/">花花酱 LeetCode Ultimate DP Study Plan Day 8</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-ultimate-dp-study-plan-day-8/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
