<?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>LCS Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/lcs/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/lcs/</link>
	<description></description>
	<lastBuildDate>Sun, 10 Jan 2021 00:38:46 +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>LCS Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/lcs/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1713. Minimum Operations to Make a Subsequence</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1713-minimum-operations-to-make-a-subsequence/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1713-minimum-operations-to-make-a-subsequence/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 03 Jan 2021 19:57:02 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[LCS]]></category>
		<category><![CDATA[LIS]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7901</guid>

					<description><![CDATA[<p>You are given an array&#160;target&#160;that consists of&#160;distinct&#160;integers and another integer array&#160;arr&#160;that&#160;can&#160;have duplicates. In one operation, you can insert any integer at any position in&#160;arr. For&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1713-minimum-operations-to-make-a-subsequence/">花花酱 LeetCode 1713. Minimum Operations to Make a Subsequence</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="【LCS/LIS】花花酱 LeetCode 1713. Minimum Operations to Make a Subsequence - 刷题找工作 EP379" width="500" height="281" src="https://www.youtube.com/embed/5vBPESNPEu4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given an array&nbsp;<code>target</code>&nbsp;that consists of&nbsp;<strong>distinct</strong>&nbsp;integers and another integer array&nbsp;<code>arr</code>&nbsp;that&nbsp;<strong>can</strong>&nbsp;have duplicates.</p>



<p>In one operation, you can insert any integer at any position in&nbsp;<code>arr</code>. For example, if&nbsp;<code>arr = [1,4,1,2]</code>, you can add&nbsp;<code>3</code>&nbsp;in the middle and make it&nbsp;<code>[1,4,<u>3</u>,1,2]</code>. Note that you can insert the integer at the very beginning or end of the array.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>minimum</strong>&nbsp;number of operations needed to make&nbsp;</em><code>target</code><em>&nbsp;a&nbsp;<strong>subsequence</strong>&nbsp;of&nbsp;</em><code>arr</code><em>.</em></p>



<p>A&nbsp;<strong>subsequence</strong>&nbsp;of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements&#8217; relative order. For example,&nbsp;<code>[2,7,4]</code>&nbsp;is a subsequence of&nbsp;<code>[4,<u>2</u>,3,<u>7</u>,2,1,<u>4</u>]</code>&nbsp;(the underlined elements), while&nbsp;<code>[2,4,2]</code>&nbsp;is not.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = [5,1,3], <code>arr</code> = [9,4,2,3,4]
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can add 5 and 1 in such a way that makes <code>arr</code> = [5,9,4,1,2,3,4], then target will be a subsequence of <code>arr</code>.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = [6,4,8,1,3,2], <code>arr</code> = [4,7,6,2,3,8,6,1]
<strong>Output:</strong> 3
</pre>



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



<ul><li><code>1 &lt;= target.length, arr.length &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= target[i], arr[i] &lt;= 10<sup>9</sup></code></li><li><code>target</code>&nbsp;contains no duplicates.</li></ul>



<h2><strong>Solution: Reduce to LIS</strong></h2>



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



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



<p>The original problem is a LCS (Longest common subsequence) problem that can be solved in O(n*m) time.<br>Since the elements in the target array is unique, we can convert the numbers into indices that helps to reduce the problem to LIS (Longest increasing subsequence) that can be solved in O(mlogn) time.</p>



<p>e.g. <br>target: [6,4,8,1,3,2] =&gt; [0, 1, 2, 3, 4, 5]<br>array: [4,7,6,2,3,8,6,1] =&gt; [1,-1, 0, 5, 4, 2, 0, 3] =&gt; [1, 0, 5, 4, 2, 3]<br>and the LIS is [0, 2, 3] =&gt; [6, 8, 1], we need to insert the rest of the numbers.<br>Ans = len(target) &#8211; len(LIS)</p>



<p>Time complexity: O(nlogm)<br>Space complexity: O(m)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int minOperations(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {
    unordered_map&lt;int, int&gt; m;
    for (int i = 0; i &lt; A.size(); ++i)
      m[A[i]] = i;
    vector&lt;int&gt; dp;
    for (int x : B) {
      auto mit = m.find(x);
      if (mit == end(m)) continue;
      const int idx = mit-&gt;second;
      if (dp.empty() || idx &gt; dp.back())
        dp.push_back(idx);
      else
        *lower_bound(begin(dp), end(dp), idx) = idx;
    }
    return A.size() - dp.size();
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:  
  def minOperations(self, A: List[int], B: List[int]) -&gt; int:
    m = {x: i for i, x in enumerate(A)}
    dp = []
    for x in B:
      if x not in m: continue
      if not dp or m[x] &gt; dp[-1]: dp.append(m[x])
      else: dp[bisect_left(dp, m[x])] = m[x]
    return len(A) - len(dp)</pre>
</div></div>



<p><br></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1713-minimum-operations-to-make-a-subsequence/">花花酱 LeetCode 1713. Minimum Operations to Make a Subsequence</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-1713-minimum-operations-to-make-a-subsequence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1143. Longest Common Subsequence</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1143-longest-common-subsequence/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1143-longest-common-subsequence/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 30 Aug 2019 06:24:35 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[dynamic programming]]></category>
		<category><![CDATA[LCS]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(mn)]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5504</guid>

					<description><![CDATA[<p>Given two strings&#160;text1&#160;and&#160;text2, return the length of their longest common subsequence. A&#160;subsequence&#160;of a string is a new string generated from the original string with some&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1143-longest-common-subsequence/">花花酱 LeetCode 1143. Longest Common Subsequence</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>Given two strings&nbsp;<code>text1</code>&nbsp;and&nbsp;<code>text2</code>, return the length of their longest common subsequence.</p>



<p>A&nbsp;<em>subsequence</em>&nbsp;of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, &#8220;ace&#8221; is a subsequence of &#8220;abcde&#8221; while &#8220;aec&#8221; is not).&nbsp;A&nbsp;<em>common subsequence</em>&nbsp;of two strings is a subsequence that is common to both strings.</p>



<p>If there is no common subsequence, return 0.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> text1 = "abcde", text2 = "ace" 
<strong>Output:</strong> 3  
<strong>Explanation:</strong> The longest common subsequence is "ace" and its length is 3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> text1 = "abc", text2 = "abc"
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest common subsequence is "abc" and its length is 3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> text1 = "abc", text2 = "def"
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no such common subsequence, so the result is 0.
</pre>



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



<ul><li><code>1 &lt;= text1.length &lt;= 1000</code></li><li><code>1 &lt;= text2.length &lt;= 1000</code></li><li>The input strings consist of lowercase English characters only.</li></ul>



<p><strong>Solution: DP</strong></p>



<p>Use dp[i][j] to represent the length of longest common sub-sequence of text1[0:i] and text2[0:j]<br>dp[i][j] = dp[i &#8211; 1][j &#8211; 1]  + 1 if text1[i &#8211; 1] == text2[j &#8211; 1] else max(dp[i][j &#8211; 1], dp[i &#8211; 1][j])</p>



<p>Time complexity: O(mn)<br>Space complexity: O(mn) -&gt; O(n)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua, 16ms 14.9 MB
class Solution {
public:
  int longestCommonSubsequence(string text1, string text2) {
    int m = text1.length();
    int n = text2.length();
    vector&lt;vector&lt;int&gt;&gt; dp(m + 1, vector&lt;int&gt;(n + 1));
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j)
        if (text1[i] == text2[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
        else
          dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);      
    return dp[m][n];
  }
};</pre>

</div><h2 class="tabtitle">C++/V2</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 8ms, 8.8MB
class Solution {
public:
  int longestCommonSubsequence(string text1, string text2) {
    int m = text1.length();
    int n = text2.length();    
    vector&lt;int&gt; dp(n + 1);    
    for (int i = 0; i &lt; m; ++i) {
      int prev = 0; // dp[i][j]
      for (int j = 0; j &lt; n; ++j) {        
        int curr = dp[j + 1]; // dp[i][j + 1]
        if (text1[i] == text2[j])
          // dp[i + 1][j + 1] = dp[i][j] + 1
          dp[j + 1] = prev + 1; 
        else
          // dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j])
          dp[j + 1] = max(curr, dp[j]);
        prev = curr;
      }    
    }    
    return dp[n]; // dp[m][n]
  }
};</pre>

</div><h2 class="tabtitle">C++/V3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int longestCommonSubsequence(string text1, string text2) {
    int m = text1.length();
    int n = text2.length();    
    vector&lt;int&gt; dp1(n + 1);
    vector&lt;int&gt; dp2(n + 1);
    for (int i = 0; i &lt; m; ++i) {      
      for (int j = 0; j &lt; n; ++j)
        if (text1[i] == text2[j])
          dp2[j + 1] = dp1[j] + 1; 
        else          
          dp2[j + 1] = max(dp1[j + 1], dp2[j]);              
      swap(dp1, dp2);
    }    
    return dp1[n];
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1143-longest-common-subsequence/">花花酱 LeetCode 1143. Longest Common Subsequence</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-1143-longest-common-subsequence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1092. Shortest Common Supersequence</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1092-shortest-common-supersequence/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1092-shortest-common-supersequence/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 16 Jun 2019 07:59:27 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[LCS]]></category>
		<category><![CDATA[subsequence]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5229</guid>

					<description><![CDATA[<p>Given two strings&#160;str1&#160;and&#160;str2,&#160;return the shortest string that has both&#160;str1&#160;and&#160;str2&#160;as subsequences.&#160;&#160;If multiple answers exist, you may return any of them. (A string S is a subsequence&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1092-shortest-common-supersequence/">花花酱 LeetCode 1092. Shortest Common Supersequence</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 width="500" height="375" src="https://www.youtube.com/embed/rfV2BJp8YA8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given two strings&nbsp;<code>str1</code>&nbsp;and&nbsp;<code>str2</code>,&nbsp;return the shortest string that has both&nbsp;<code>str1</code>&nbsp;and&nbsp;<code>str2</code>&nbsp;as subsequences.&nbsp;&nbsp;If multiple answers exist, you may return any of them.</p>



<p><em>(A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen&nbsp;anywherefrom T) results in the string S.)</em></p>



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



<pre class="wp-block-preformatted; crayon:false"><strong>Input: </strong>str1 = "abac", str2 = "cab"
<strong>Output: </strong>"cabac"
<strong>Explanation: </strong>
str1 = "abac" is a substring of "cabac" because we can delete the first "c".
str2 = "cab" is a substring of "cabac" because we can delete the last "ac".
The answer provided is the shortest such string that satisfies these properties.
</pre>



<p><strong>Note:</strong></p>



<ol><li><code>1 &lt;= str1.length, str2.length &lt;= 1000</code></li><li><code>str1</code>&nbsp;and&nbsp;<code>str2</code>&nbsp;consist of lowercase English letters.</li></ol>



<figure class="wp-block-image"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/06/1092-ep251.png" alt="" class="wp-image-5232" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/06/1092-ep251.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/06/1092-ep251-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/06/1092-ep251-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<h2><strong>Solution: LCS </strong></h2>



<p>Find the LCS (longest common sub-sequence) of two strings, and insert unmatched characters into the LCS.</p>



<p>Time complexity: O(mn)<br>Space complexity: O(mn)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua, running time: 16 ms, 15.3 MB
class Solution {
public:
  string shortestCommonSupersequence(string str1, string str2) {
    int l1 = str1.length();
    int l2 = str2.length();    
    vector&lt;vector&lt;int&gt;&gt; dp(l1 + 1, vector&lt;int&gt;(l2 + 1));    
    for (int i = 1; i &lt;= l1; ++i)
      for (int j = 1; j &lt;= l2; ++j)
        dp[i][j] = str1[i - 1] == str2[j - 1] ? 
                     1 + dp[i - 1][j - 1] : 
                     max(dp[i - 1][j], dp[i][j - 1]);
    deque&lt;char&gt; ans;
    while (l1 || l2) {
      char c;
      if (l1 == 0) c = str2[--l2];
      else if (l2 == 0) c = str1[--l1];
      else if (str1[l1 - 1] == str2[l2 - 1]) c = str1[--l1] = str2[--l2];
      else if (dp[l1 - 1][l2] == dp[l1][l2]) c = str1[--l1];
      else if (dp[l1][l2 - 1] == dp[l1][l2]) c = str2[--l2];
      ans.push_front(c);
    }    
    return {begin(ans), end(ans)};
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1092-shortest-common-supersequence/">花花酱 LeetCode 1092. Shortest Common Supersequence</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-1092-shortest-common-supersequence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 674. Longest Continuous Increasing Subsequence</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-674-longest-continuous-increasing-subsequence/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-674-longest-continuous-increasing-subsequence/#comments</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Sep 2017 15:24:29 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[LC]]></category>
		<category><![CDATA[LCIS]]></category>
		<category><![CDATA[LCS]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=199</guid>

					<description><![CDATA[<p>Problem: Given an unsorted array of integers, find the length of longest continuous increasing subsequence. Example 1: [crayon-663c613d25957064863161/] Explanation: The longest continuous increasing subsequence is [1,3,5], its&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-674-longest-continuous-increasing-subsequence/">花花酱 LeetCode 674. Longest Continuous Increasing Subsequence</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/I_bxq4nm2sk?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<div class="question-description">
<p>Given an unsorted array of integers, find the length of longest <code>continuous</code> increasing subsequence.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: [1,3,5,4,7]
Output: 3</pre><p>Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even though [1,3,5,7] is also an increasing subsequence, it&#8217;s not a continuous one where 5 and 7 are separated by 4.</p>
<p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: [2,2,2,2,2]
Output: 1</pre><p>Explanation: The longest continuous increasing subsequence is [2], its length is 1.</p>
<p><b>Note:</b> Length of the array will not exceed 10,000.</p>
</div>
<div id="interviewed-div"><strong>Idea:</strong></div>
<div></div>
<div>Dynamic Programming</div>
<div></div>
<div></div>
<div><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/674-ep47.png"><img class="alignnone size-full wp-image-200" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/674-ep47.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/674-ep47.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/674-ep47-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/674-ep47-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/674-ep47-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></div>
<div></div>
<div><strong>Solution:</strong></div>
<div>
<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    int findLengthOfLCIS(vector&lt;int&gt;&amp; nums) {
        if (nums.empty()) return 0;
        int cur = 1;
        int ans = 1;
        for (int i = 1; i &lt; nums.size(); ++i) {
            if (nums[i] &gt; nums[i-1]) {
                ++cur;
                ans = max(ans, cur);
            } else {
                cur = 1;
            }            
        }
        return ans;
    }
};</pre><br />
&nbsp;</p>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-674-longest-continuous-increasing-subsequence/">花花酱 LeetCode 674. Longest Continuous Increasing Subsequence</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-674-longest-continuous-increasing-subsequence/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
