<?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>coding with me Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/coding-with-me/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/coding-with-me/</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>coding with me Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/coding-with-me/</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-663cb322961a4080297043/] 714 Best Time to Buy and Sell Stock with Transaction Fee [crayon-663cb322961ad349701500/]</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>
		<item>
		<title>花花酱 LeetCode Ultimate DP Study Plan Day 6</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-6/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-6/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 02 Dec 2021 04:48:06 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[coding with me]]></category>
		<category><![CDATA[dp]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8957</guid>

					<description><![CDATA[<p>152.&#160;Maximum Product Subarray [crayon-663cb322966dc605329054/] 1567. Maximum Length of Subarray With Positive Product [crayon-663cb322966df055627845/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-6/">花花酱 LeetCode Ultimate DP Study Plan Day 6</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终极学习计划！Day6 | Maximum Product Subarray【跟我一起写代码】" width="500" height="281" src="https://www.youtube.com/embed/gwZm6mIYDfk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<h2><strong>152.&nbsp;Maximum Product Subarray</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) -&gt; O(1)
class Solution:
  def maxProduct(self, nums: List[int]) -&gt; int:
    
    @cache
    def dp(i: int) -&gt; Tuple[int, int]:
      &quot;&quot;&quot;min / max subarray product ends with nums[i].&quot;&quot;&quot;
      if i == 0: return nums[i], nums[i]
      low, hi = dp(i - 1)
      if nums[i] &lt; 0: low, hi = hi, low
      return min(low * nums[i], nums[i]), max(hi * nums[i], nums[i])
    
    return max(dp(i)[1] for i in range(len(nums)))</pre>
</div></div>



<h2><strong>1567. Maximum Length of Subarray With Positive Product</strong></h2>



<figure class="wp-block-image size-large"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/dp-day6-1.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/dp-day6-1.png" alt="" class="wp-image-8997" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/dp-day6-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/dp-day6-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/dp-day6-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<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) -&gt; O(1)
class Solution:
  def getMaxLen(self, nums: List[int]) -&gt; int:
    
    @cache
    def dp(i: int) -&gt; Tuple[int, int]:
      &quot;&quot;&quot;Max length of pos/neg product ends with nums[i].&quot;&quot;&quot;
      if i &lt; 0 or nums[i] == 0: return 0, 0
      p, n = dp(i - 1)
      if nums[i] &gt; 0:
        return p + 1, n + 1 if n else 0
      else: # nums[i] &lt; 0
        return n + 1 if n else 0, p + 1
    
    return max(dp(i)[0] for i in range(len(nums)))</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-6/">花花酱 LeetCode Ultimate DP Study Plan Day 6</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-6/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode Ultimate DP Study Plan Day 5</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-5/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-5/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 01 Dec 2021 18:37:13 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[coding with me]]></category>
		<category><![CDATA[dp]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8954</guid>

					<description><![CDATA[<p>53. Maximum Subarray [crayon-663cb3229690f397333972/] 918. Maximum Sum Circular Subarray [crayon-663cb32296912379733325/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-5/">花花酱 LeetCode Ultimate DP Study Plan Day 5</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终极学习计划！Day5 Maximum Subarray【跟我一起写代码】" width="500" height="281" src="https://www.youtube.com/embed/Gk6yWhfzdOc?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<h2><strong>53. Maximum Subarray</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 maxSubArray(self, nums: List[int]) -&gt; int:
    
    @cache
    def dp(i: int) -&gt; int:
      &quot;&quot;&quot;Max subarray ends with nums[i].&quot;&quot;&quot;
      if i &lt; 0: return 0 # Empty array.
      return nums[i] + max(0, dp(i - 1))
    
    # Try all possible ending positions.
    return max(dp(i) for i in range(len(nums)))</pre>
</div></div>



<h2><strong>918. Maximum Sum Circular Subarray</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 maxSubarraySumCircular(self, nums: List[int]) -&gt; int:
    n = len(nums)
    
    @cache
    def dp(i: int, s: int) -&gt; int:
      &quot;&quot;&quot;Max subarray ends with s * nums[i].&quot;&quot;&quot;
      if i &lt; 0: return 0 # Empty array.
      # Append to max subbary ends with nums[i-1] or start a new one.
      return nums[i] * s + max(0, dp(i - 1, s))
    
    max_p = max(dp(i, 1) for i in range(n))
    max_n = max(dp(i, -1) for i in range(n))
    
    return max_p if max_p &lt; 0 else max(max_p, sum(nums) + max_n)</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-5/">花花酱 LeetCode Ultimate DP Study Plan Day 5</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-5/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode Ultimate DP Study Plan Day 4</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-4/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-4/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 01 Dec 2021 16:43:09 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[coding with me]]></category>
		<category><![CDATA[dp]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8951</guid>

					<description><![CDATA[<p>55 Jump Game [crayon-663cb32296c9e294135133/] 45 Jump Game II [crayon-663cb32296ca1138950995/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-4/">花花酱 LeetCode Ultimate DP Study Plan Day 4</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终极学习计划！Day4 Jump Game I/II 【跟我一起写代码】" width="500" height="281" src="https://www.youtube.com/embed/3mIc_mKP4yM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<h2><strong>55 Jump Game</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^2)
# Space complexity: O(n)
class Solution:
  def canJump(self, nums: List[int]) -&gt; bool:
    n = len(nums)
    
    @cache
    def dp(i: int) -&gt; bool:
      &quot;&quot;&quot;Returns whether we can reach n - 1 from i.&quot;&quot;&quot;
      if i &gt;= n - 1: return True
      if i + nums[i] &gt;= n - 1: return True
      for s in range(1, nums[i] + 1):
        if dp(i + s): return True
      return False
    
    return dp(0)</pre>
</div></div>



<h2><strong>45 Jump Game 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^2)
# Space complexity: O(n)
class Solution:
  def jump(self, nums: List[int]) -&gt; int:
    n = len(nums)
    
    @cache
    def dp(i: int) -&gt; int:
      &quot;&quot;&quot;Min steps to reach n - 1 from i.&quot;&quot;&quot;
      if i &gt;= n - 1: return 0
      if i + nums[i] &gt;= n - 1: return 1
      ans = n
      for s in range(1, nums[i] + 1):
        ans = min(ans, dp(i + s) + 1)
      return ans
    
    return dp(0)</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-4/">花花酱 LeetCode Ultimate DP Study Plan Day 4</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-4/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode Ultimate DP Study Plan Day 1 &#8211; 3</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-1-3/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-1-3/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 01 Dec 2021 07:10:29 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[coding with me]]></category>
		<category><![CDATA[dp]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8943</guid>

					<description><![CDATA[<p>Day 1 509. Fibonacci Number [crayon-663cb32296ed8469118071/] 1137. N-th Tribonacci Number [crayon-663cb32296eda308658938/] Day 2 70. Climbing Stairs [crayon-663cb32296edb309411929/] 746. Min Cost Climbing Stairs [crayon-663cb32296edc047364105/] Day 3 198. House Robber [crayon-663cb32296ede514192206/] 213. House&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-ultimate-dp-study-plan-day-1-3/">花花酱 LeetCode Ultimate DP Study Plan Day 1 &#8211; 3</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终极学习计划！Day1-3" width="500" height="281" src="https://www.youtube.com/embed/LG0adDNnnYg?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<h2><strong>Day 1</strong></h2>



<ul><li>509. Fibonacci Number</li></ul>



<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) -&gt; O(1)
class Solution:
  @cache
  def fib(self, n: int) -&gt; int:
    return n if n &lt;= 1 else self.fib(n - 1) + self.fib(n - 2)</pre>
</div></div>



<ul><li>1137. N-th Tribonacci Number</li></ul>



<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) -&gt; O(1)
class Solution:
  @cache
  def tribonacci(self, n: int) -&gt; int:
    if n == 0: return 0
    if n &lt;= 2: return 1
    return sum(self.tribonacci(n - i - 1) for i in range(3))</pre>
</div></div>



<h2><strong>Day 2</strong></h2>



<ul><li>70. Climbing Stairs</li></ul>



<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) -&gt; O(1)
class Solution:
  @cache
  def climbStairs(self, n: int) -&gt; int:
    if n &lt;= 1: return 1
    return self.climbStairs(n - 1) + self.climbStairs(n - 2)</pre>
</div></div>



<ul><li>746. Min Cost Climbing Stairs</li></ul>



<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) -&gt; O(1)
class Solution:
  def minCostClimbingStairs(self, cost: List[int]) -&gt; int:
    @cache
    def dp(i: int) -&gt; int:
      if i &lt;= 1: return 0
      return min(dp(i - 1) + cost[i - 1],
                 dp(i - 2) + cost[i - 2])
    return dp(len(cost))</pre>
</div></div>



<h2><strong>Day 3</strong></h2>



<ul><li>198. House Robber</li></ul>



<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) -&gt; O(1)
class Solution:  
  def rob(self, nums: List[int]) -&gt; int:
    @cache
    def dp(i: int) -&gt; int:
      if i &lt; 0: return 0
      return max(nums[i] + dp(i - 2), dp(i - 1))
    return dp(len(nums) - 1)</pre>
</div></div>



<ul><li>213. House Robber II</li></ul>



<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) -&gt; O(1)
class Solution:
  def rob(self, nums: List[int]) -&gt; int:
    n = len(nums)
    if n == 1: return nums[0]

    @cache
    def dp(i: int, j: int) -&gt; int:
      if j &lt; i: return 0
      return max(nums[j] + dp(i, j - 2), dp(i, j - 1))

    return max(dp(0, n - 2), dp(1, n - 1))</pre>
</div></div>



<ul><li>740. Delete and Earn</li></ul>



<p>This problem can be reduced to LC 198 House Robber</p>



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

<pre class="crayon-plain-tag"># Author: Huahua
# Time complexity: O(m + n)
# Space complexity: O(m + n)
class Solution:
  def deleteAndEarn(self, nums: List[int]) -&gt; int:
    houses = [0] * (10**4 + 1)
    for x in nums:
      houses[x] += x
    
    @cache
    def dp(i : int) -&gt; int:
      if i &lt; 0: return 0
      return max(houses[i] + dp(i - 2), dp(i - 1))
    
    return dp(len(houses) - 1)</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-1-3/">花花酱 LeetCode Ultimate DP Study Plan Day 1 &#8211; 3</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-1-3/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
