<?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>O(n*k) Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/onk/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/onk/</link>
	<description></description>
	<lastBuildDate>Sun, 02 Feb 2020 08:34:55 +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>O(n*k) Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/onk/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1344. Jump Game V</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1344-jump-game-v/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1344-jump-game-v/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 02 Feb 2020 07:15:58 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[O(n*k)]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6237</guid>

					<description><![CDATA[<p>Given an array of&#160;integers&#160;arr&#160;and an integer&#160;d. In one step you can jump from index&#160;i&#160;to index: i + x&#160;where:&#160;i + x &#60; arr.length&#160;and&#160;0 &#60;&#160;x &#60;= d.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1344-jump-game-v/">花花酱 LeetCode 1344. Jump Game V</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-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1344. Jump Game V - 刷题找工作 EP305" width="500" height="375" src="https://www.youtube.com/embed/y5hRO6NvOHg?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given an array of&nbsp;integers&nbsp;<code>arr</code>&nbsp;and an integer&nbsp;<code>d</code>. In one step you can jump from index&nbsp;<code>i</code>&nbsp;to index:</p>



<ul><li><code>i + x</code>&nbsp;where:&nbsp;<code>i + x &lt; arr.length</code>&nbsp;and&nbsp;<code>0 &lt;&nbsp;x &lt;= d</code>.</li><li><code>i - x</code>&nbsp;where:&nbsp;<code>i - x &gt;= 0</code>&nbsp;and&nbsp;<code>0 &lt;&nbsp;x &lt;= d</code>.</li></ul>



<p>In addition, you can only jump from index&nbsp;<code>i</code>&nbsp;to index&nbsp;<code>j</code>&nbsp;if&nbsp;<code>arr[i] &gt; arr[j]</code>&nbsp;and&nbsp;<code>arr[i] &gt; arr[k]</code>&nbsp;for all indices&nbsp;<code>k</code>&nbsp;between&nbsp;<code>i</code>&nbsp;and&nbsp;<code>j</code>&nbsp;(More formally&nbsp;<code>min(i,&nbsp;j) &lt; k &lt; max(i, j)</code>).</p>



<p>You can choose any index of the array and start jumping. Return&nbsp;<em>the maximum number of indices</em>&nbsp;you can visit.</p>



<p>Notice that you can not jump outside of the array at any time.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/01/23/meta-chart.jpeg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> You can start at index 10. You can jump 10 --&gt; 8 --&gt; 6 --&gt; 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 &gt; 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 &gt; 9.
Similarly You cannot jump from index 3 to index 2 or index 1.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [3,3,3,3,3], d = 3
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can start at any index. You always cannot jump to any index.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [7,6,5,4,3,2,1], d = 1
<strong>Output:</strong> 7
<strong>Explanation:</strong> Start at index 0. You can visit all the indicies. 
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [7,1,7,1,7,1], d = 2
<strong>Output:</strong> 2
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [66], d = 1
<strong>Output:</strong> 1
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= arr.length &lt;= 1000</code></li><li><code>1 &lt;= arr[i] &lt;= 10^5</code></li><li><code>1 &lt;= d &lt;= arr.length</code></li></ul>



<h2><strong>Solution: Recursion w/ Memoization</strong></h2>



<figure class="wp-block-image"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/02/1344-ep305-1.png" alt="" class="wp-image-6241" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/02/1344-ep305-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/02/1344-ep305-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/02/1344-ep305-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>dp(i) = max{1, max{dp(j) + 1}}, if i can jump to j.<br>ans = max(dp(i))</p>



<p>Time complexity: O(n*d)<br>Space complexity: O(n)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maxJumps(vector&lt;int&gt;&amp; a, int d) {
    int n = a.size();    
    vector&lt;int&gt; m(n);
    function&lt;int(int)&gt; dp = [&amp;](int i) {
      if (m[i]) return m[i];
      int ans = 1;
      for (int j = i + 1; j &lt;= min(n - 1, i + d) &amp;&amp; a[i] &gt; a[j]; ++j) 
        ans = max(ans, dp(j) + 1);
      for (int j = i - 1; j &gt;= max(0, i - d) &amp;&amp; a[i] &gt; a[j]; --j)
        ans = max(ans, dp(j) + 1);        
      return m[i] = ans;
    };
    
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)  
      ans = max(ans, dp(i));    
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: DP</strong></h2>



<figure class="wp-block-image"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/02/1344-ep305-2.png" alt="" class="wp-image-6242" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/02/1344-ep305-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/02/1344-ep305-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/02/1344-ep305-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>Time complexity: O(nlogn + n*d)<br>Space complexity: O(n)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maxJumps(vector&lt;int&gt;&amp; a, int d) {
    const int n = a.size();
    vector&lt;pair&lt;int, int&gt;&gt; m(n); // &lt;height, index&gt;
    for (int i = 0; i &lt; n; ++i)
      m[i] = {a[i], i};    
    // Solve from the lowest bar.
    sort(begin(m), end(m));    

    vector&lt;int&gt; dp(n, 1);
    for (auto [v, i] : m) {      
      for (int j = i + 1; j &lt;= min(n - 1, i + d) &amp;&amp; a[i] &gt; a[j]; ++j) 
        dp[i] = max(dp[i], dp[j] + 1);
      for (int j = i - 1; j &gt;= max(0, i - d) &amp;&amp; a[i] &gt; a[j]; --j)
        dp[i] = max(dp[i], dp[j] + 1);
    }
    
    return *max_element(begin(dp), end(dp));
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1344-jump-game-v/">花花酱 LeetCode 1344. Jump Game V</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-1344-jump-game-v/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
