<?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>Ultimate DP Study Plan Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/ultimate-dp-study-plan/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/ultimate-dp-study-plan/</link>
	<description></description>
	<lastBuildDate>Tue, 07 Dec 2021 02:38:18 +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>Ultimate DP Study Plan Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/ultimate-dp-study-plan/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode Ultimate DP Study Plan Day 7</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-7/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-7/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 06 Dec 2021 04:48:59 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[Ultimate DP Study Plan]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9059</guid>

					<description><![CDATA[<p>1014.&#160;Best Sightseeing Pair [crayon-6665f70807e4e338062352/] 121.&#160;Best Time to Buy and Sell Stock [crayon-6665f70807e52691371131/] 122.&#160;Best Time to Buy and Sell Stock II [crayon-6665f70807e53921623313/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-7/">花花酱 LeetCode Ultimate DP Study Plan Day 7</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-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="LeetCode DP终极学习计划！Day7 | Best Time to Buy and Sell Stock【跟我一起写代码】" width="500" height="375" src="https://www.youtube.com/embed/iTyEa35Ve-U?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<h2><strong>1014.&nbsp;Best Sightseeing Pair</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 maxScoreSightseeingPair(self, values: List[int]) -&gt; int:    
    # score = (values[i] + i) + (values[j] - j)
    @cache
    def dp(j: int) -&gt; Tuple[int, int]:
      &quot;&quot;&quot;Returns:
        1) Max score of values[0..j]
        2) Largest values[i] + i (i &lt;= j).&quot;&quot;&quot;
      if j &lt; 0: return 0, 0
      s, v = dp(j - 1)
      return max(s, v + values[j] - j), max(v, values[j] + j)
    
    return dp(len(values) - 1)[0]</pre>
</div></div>



<h2><strong>121.&nbsp;Best Time to Buy and Sell Stock</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]:
      &quot;&quot;&quot;Returns:
        1) max profit of prices[0:i].
        2) min price of prices[0:i].&quot;&quot;&quot;
      if i &lt; 0: return 0, 10**9
      max_profit, min_price = dp(i - 1)
      return max(max_profit, prices[i] - min_price), min(min_price, prices[i])

    return dp(len(prices) - 1)[0]</pre>
</div></div>



<h2><strong>122.&nbsp;Best Time to Buy and Sell Stock II</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]:
      &quot;&quot;&quot;Returns:
      1) Max profit after i-th day. Holding no stocks. Can buy.
      2) Max balance after i-th day. Holding a stock. Can sell.
      &quot;&quot;&quot;
      if i &lt; 0: return 0, -10**9
      profit, balance = dp(i - 1)
      
      return (max(profit, balance + prices[i]), # do nothing, sell
              max(balance, profit - prices[i]))  # do nothing, buy
    
    return dp(len(prices) - 1)[0]</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-7/">花花酱 LeetCode Ultimate DP Study Plan Day 7</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-7/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
