<?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>two poiners Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/two-poiners/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/two-poiners/</link>
	<description></description>
	<lastBuildDate>Sun, 02 Aug 2020 20:19:56 +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>two poiners Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/two-poiners/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1537. Get the Maximum Score</title>
		<link>https://zxi.mytechroad.com/blog/two-pointers/leetcode-1537-get-the-maximum-score/</link>
					<comments>https://zxi.mytechroad.com/blog/two-pointers/leetcode-1537-get-the-maximum-score/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 02 Aug 2020 19:52:45 +0000</pubDate>
				<category><![CDATA[Two pointers]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[two poiners]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7197</guid>

					<description><![CDATA[<p>You are given two&#160;sorted&#160;arrays of distinct integers&#160;nums1&#160;and&#160;nums2. A&#160;validpath&#160;is defined as follows: Choose&#160;array nums1 or nums2 to traverse (from index-0). Traverse the current array from left&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-1537-get-the-maximum-score/">花花酱 LeetCode 1537. Get the Maximum Score</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>You are given two&nbsp;<strong>sorted</strong>&nbsp;arrays of distinct integers&nbsp;<code>nums1</code>&nbsp;and&nbsp;<code>nums2.</code></p>



<p>A&nbsp;<strong>validpath</strong>&nbsp;is defined as follows:</p>



<ul><li>Choose&nbsp;array nums1 or nums2 to traverse (from index-0).</li><li>Traverse the current array from left to right.</li><li>If you are reading any value that is present in&nbsp;<code>nums1</code>&nbsp;and&nbsp;<code>nums2</code>&nbsp;you are allowed to change your path to the other array. (Only one repeated value is considered in the&nbsp;valid path).</li></ul>



<p><em>Score</em>&nbsp;is defined as the sum of uniques values in a valid path.</p>



<p>Return the maximum&nbsp;<em>score</em>&nbsp;you can obtain of all possible&nbsp;<strong>valid&nbsp;paths</strong>.</p>



<p>Since the answer&nbsp;may be too large,&nbsp;return it modulo&nbsp;10^9 + 7.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/07/16/sample_1_1893.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
<strong>Output:</strong> 30
<strong>Explanation:</strong>&nbsp;Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10],  (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10]    (starting from nums2)
The maximum is obtained with the path in green <strong>[2,4,6,8,10]</strong>.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums1 = [1,3,5,7,9], nums2 = [3,5,100]
<strong>Output:</strong> 109
<strong>Explanation:</strong>&nbsp;Maximum sum is obtained with the path <strong>[1,3,5,100]</strong>.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
<strong>Output:</strong> 40
<strong>Explanation:</strong>&nbsp;There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12]
<strong>Output:</strong> 61
</pre>



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



<ul><li><code>1 &lt;= nums1.length &lt;= 10^5</code></li><li><code>1 &lt;= nums2.length &lt;= 10^5</code></li><li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10^7</code></li><li><code>nums1</code>&nbsp;and&nbsp;<code>nums2</code>&nbsp;are strictly increasing.</li></ul>



<h2><strong>Solution: Two Pointers + DP</strong></h2>



<p>Since numbers are strictly increasing, we can always traverse the smaller one using two pointers. <br>Traversing ([2,4,5,8,10], [4,6,8,10])<br>will be like [2, 4/4, 5, 6, 8, 10/10]<br>It two nodes have the same value, we have two choices and pick the larger one, then both move nodes one step forward. Otherwise, the smaller node moves one step forward.<br>dp1[i] := max path sum ends with nums1[i-1]<br>dp2[j] := max path sum ends with nums2[j-1]<br>if nums[i -1] == nums[j &#8211; 1]:<br>  dp1[i] = dp2[j] = max(dp[i-1], dp[j-1]) + nums[i -1]<br>  i += 1, j += 1<br>else if nums[i &#8211; 1] &lt; nums[j &#8211; 1]:<br>  dp[i] = dp[i-1] + nums[i -1]<br>  i += 1<br>else if nums[j &#8211; 1] &lt; nums[i &#8211; 1]:<br>  dp[j] = dp[j-1] + nums[j -1]<br>  j += 1<br>return max(dp1[-1], dp2[-1])</p>



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  int maxSum(vector&lt;int&gt;&amp; nums1, vector&lt;int&gt;&amp; nums2) {
    constexpr int kMod = 1e9 + 7;
    const int n1 = nums1.size();
    const int n2 = nums2.size();
    vector&lt;long&gt; dp1(n1 + 1); // max path sum ends with nums1[i-1]
    vector&lt;long&gt; dp2(n2 + 1); // max path sum ends with nums2[j-1]
    int i = 0;
    int j = 0;
    while (i &lt; n1 || j &lt; n2) {
      if (i &lt; n1 &amp;&amp; j &lt; n2 &amp;&amp; nums1[i] == nums2[j]) {
        // Same, two choices, pick the larger one.
        dp2[j + 1] = dp1[i + 1] = max(dp1[i], dp2[j]) + nums1[i];
        ++i; ++j;
      } else if (i &lt; n1 &amp;&amp; (j == n2 || nums1[i] &lt; nums2[j])) {
        dp1[i + 1] = dp1[i] + nums1[i];
        ++i;
      } else if (j &lt; n2 &amp;&amp; (i == n1 || nums2[j] &lt; nums1[i])) {        
        dp2[j + 1] = dp2[j] + nums2[j];
        ++j;
      }
    }    
    return max(dp1.back(), dp2.back()) % kMod;
  }
};</pre>

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

<pre class="crayon-plain-tag">class Solution {
  public int maxSum(int[] nums1, int[] nums2) {
    final int kMod = 1_000_000_007;
    int n1 = nums1.length;
    int n2 = nums2.length;
    int i = 0;
    int j = 0;
    long a = 0;
    long b = 0;
    while (i &lt; n1 || j &lt; n2) {
      if (i &lt; n1 &amp;&amp; j &lt; n2 &amp;&amp; nums1[i] == nums2[j]) {
        a = b = Math.max(a, b) + nums1[i];
        ++i;
        ++j;
      } else if (i &lt; n1 &amp;&amp; (j == n2 || nums1[i] &lt; nums2[j])) {
        a += nums1[i++];        
      } else {
        b += nums2[j++];
      }
    }
    return (int)(Math.max(a, b) % kMod);
  }
}</pre>

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

<pre class="crayon-plain-tag">class Solution:
  def maxSum(self, nums1: List[int], nums2: List[int]) -&gt; int:
    n1, n2 = len(nums1), len(nums2)
    i, j = 0, 0
    a, b = 0, 0
    while i &lt; n1 or j &lt; n2:
      if i &lt; n1 and j &lt; n2 and nums1[i] == nums2[j]:
        a = b = max(a, b) + nums1[i]
        i += 1
        j += 1
      elif i &lt; n1 and (j == n2 or nums1[i] &lt; nums2[j]):
        a += nums1[i]
        i += 1
      else:
        b += nums2[j]
        j += 1
    return max(a, b) % (10**9 + 7)</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-1537-get-the-maximum-score/">花花酱 LeetCode 1537. Get the Maximum Score</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/two-pointers/leetcode-1537-get-the-maximum-score/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
