<?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 sum Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/two-sum/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/two-sum/</link>
	<description></description>
	<lastBuildDate>Sat, 03 Apr 2021 19:06:08 +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 sum Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/two-sum/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1814. Count Nice Pairs in an Array</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1814-count-nice-pairs-in-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1814-count-nice-pairs-in-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 03 Apr 2021 19:05:45 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[two sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8303</guid>

					<description><![CDATA[<p>You are given an array&#160;nums&#160;that consists of non-negative integers. Let us define&#160;rev(x)&#160;as the reverse of the non-negative integer&#160;x. For example,&#160;rev(123) = 321, and&#160;rev(120) = 21.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1814-count-nice-pairs-in-an-array/">花花酱 LeetCode 1814. Count Nice Pairs in an Array</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 an array&nbsp;<code>nums</code>&nbsp;that consists of non-negative integers. Let us define&nbsp;<code>rev(x)</code>&nbsp;as the reverse of the non-negative integer&nbsp;<code>x</code>. For example,&nbsp;<code>rev(123) = 321</code>, and&nbsp;<code>rev(120) = 21</code>. A pair of indices&nbsp;<code>(i, j)</code>&nbsp;is&nbsp;<strong>nice</strong>&nbsp;if it satisfies all of the following conditions:</p>



<ul><li><code>0 &lt;= i &lt; j &lt; nums.length</code></li><li><code>nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])</code></li></ul>



<p>Return&nbsp;<em>the number of nice pairs of indices</em>. Since that number can be too large, return it&nbsp;<strong>modulo</strong>&nbsp;<code>10<sup>9</sup>&nbsp;+ 7</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [42,11,1,97]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The two pairs are:
 - (0,3) : 42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121.
 - (1,2) : 11 + rev(1) = 11 + 1 = 12, 1 + rev(11) = 1 + 11 = 12.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [13,10,35,24,76]
<strong>Output:</strong> 4
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li></ul>



<h2><strong>Solution: Two Sum</strong></h2>



<p>Key = x &#8211; rev(x)</p>



<p>Time complexity: O(n)<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 countNicePairs(vector&lt;int&gt;&amp; nums) {
    constexpr int kMod = 1e9 + 7;
    unordered_map&lt;int, int&gt; m;
    long ans = 0;
    for (int x : nums) {      
      string s = to_string(x);
      reverse(begin(s), end(s));
      ans += m[x - stoi(s)]++;
    }
    return ans % kMod;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1814-count-nice-pairs-in-an-array/">花花酱 LeetCode 1814. Count Nice Pairs in an Array</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/hashtable/leetcode-1814-count-nice-pairs-in-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1726. Tuple with Same Product</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1726-tuple-with-same-product/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1726-tuple-with-same-product/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 Jan 2021 20:49:07 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[two sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7986</guid>

					<description><![CDATA[<p>Given an array&#160;nums&#160;of&#160;distinct&#160;positive integers, return&#160;the number of tuples&#160;(a, b, c, d)&#160;such that&#160;a * b = c * d&#160;where&#160;a,&#160;b,&#160;c, and&#160;d&#160;are elements of&#160;nums, and&#160;a != b !=&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1726-tuple-with-same-product/">花花酱 LeetCode 1726. Tuple with Same Product</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 an array&nbsp;<code>nums</code>&nbsp;of&nbsp;<strong>distinct</strong>&nbsp;positive integers, return&nbsp;<em>the number of tuples&nbsp;</em><code>(a, b, c, d)</code><em>&nbsp;such that&nbsp;</em><code>a * b = c * d</code><em>&nbsp;where&nbsp;</em><code>a</code><em>,&nbsp;</em><code>b</code><em>,&nbsp;</em><code>c</code><em>, and&nbsp;</em><code>d</code><em>&nbsp;are elements of&nbsp;</em><code>nums</code><em>, and&nbsp;</em><code>a != b != c != d</code><em>.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,3,4,6]
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are 8 valid tuples:
(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)
(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,4,5,10]
<strong>Output:</strong> 16
<strong>Explanation:</strong> There are 16 valids tuples:
(1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2)
(2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1)
(2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,4,5)
(4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2)
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,3,4,6,8,12]
<strong>Output:</strong> 40
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,3,5,7]
<strong>Output:</strong> 0
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 1000</code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li><li>All elements in&nbsp;<code>nums</code>&nbsp;are&nbsp;<strong>distinct</strong>.</li></ul>



<h2><strong>Solution: HashTable</strong></h2>



<p>Similar idea to <a href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1-two-sum/" data-type="post" data-id="903">花花酱 LeetCode 1. Two Sum</a></p>



<p>Use a hashtable to store all the pair product counts.</p>



<p>Enumerate all possible pairs, increase the answer by the same product counts * 8.</p>



<p>Why time 8? C(4,1) * C(1,1) * C(2,1) * C(1,1)</p>



<p>For pair one AxB, A can be placed at any position in a four tuple, B&#8217;s position is then fixed. For another pair CxD, C has two positions to choose from, D is fixed.</p>



<p>Time complexity: O(n^2)<br>Space complexity: O(n^2)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int tupleSameProduct(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    unordered_map&lt;int, int&gt; m;
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = 0; j &lt; i; ++j)        
        ans += 8 * m[nums[i] * nums[j]]++;
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1726-tuple-with-same-product/">花花酱 LeetCode 1726. Tuple with Same Product</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/hashtable/leetcode-1726-tuple-with-same-product/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1711. Count Good Meals</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1711-count-good-meals/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1711-count-good-meals/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 03 Jan 2021 07:35:38 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[pairs]]></category>
		<category><![CDATA[target sum]]></category>
		<category><![CDATA[two sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7893</guid>

					<description><![CDATA[<p>A&#160;good meal&#160;is a meal that contains&#160;exactly two different food items&#160;with a sum of deliciousness equal to a power of two. You can pick&#160;any&#160;two different foods&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1711-count-good-meals/">花花酱 LeetCode 1711. Count Good Meals</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>A&nbsp;<strong>good meal</strong>&nbsp;is a meal that contains&nbsp;<strong>exactly two different food items</strong>&nbsp;with a sum of deliciousness equal to a power of two.</p>



<p>You can pick&nbsp;<strong>any</strong>&nbsp;two different foods to make a good meal.</p>



<p>Given an array of integers&nbsp;<code>deliciousness</code>&nbsp;where&nbsp;<code>deliciousness[i]</code>&nbsp;is the deliciousness of the&nbsp;<code>i<sup>​​​​​​th</sup>​​​​</code>​​​​ item of food, return&nbsp;<em>the number of different&nbsp;<strong>good meals</strong>&nbsp;you can make from this list modulo</em>&nbsp;<code>10<sup>9</sup>&nbsp;+ 7</code>.</p>



<p>Note that items with different indices are considered different even if they have the same deliciousness value.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> deliciousness = [1,3,5,7,9]
<strong>Output:</strong> 4
<strong>Explanation: </strong>The good meals are (1,3), (1,7), (3,5) and, (7,9).
Their respective sums are 4, 8, 8, and 16, all of which are powers of 2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> deliciousness = [1,1,1,3,3,3,7]
<strong>Output:</strong> 15
<strong>Explanation: </strong>The good meals are (1,1) with 3 ways, (1,3) with 9 ways, and (1,7) with 3 ways.</pre>



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



<ul><li><code>1 &lt;= deliciousness.length &lt;= 10<sup>5</sup></code></li><li><code>0 &lt;= deliciousness[i] &lt;= 2<sup>20</sup></code></li></ul>



<h2><strong>Solution: Hashtable</strong></h2>



<p>Same idea as LeetCode 1: Two Sum</p>



<p>Use a hashtable to store the occurrences of all the numbers added so far. For a  new number x, check all possible 2^i &#8211; x. ans += freq[2^i &#8211; x] 0 &lt;= i &lt;= 21</p>



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



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

<pre class="crayon-plain-tag">class Solution {
public:
  int countPairs(vector&lt;int&gt;&amp; A) {
    constexpr int kMod = 1e9 + 7;
    unordered_map&lt;int, int&gt; m;    
    long ans = 0;
    for (int x : A) {
      for (int t = 1; t &lt;= 1 &lt;&lt; 21; t *= 2) {
        auto it = m.find(t - x);
        if (it != end(m)) ans += it-&gt;second;
      }
      ++m[x];
    }
    return ans % kMod;
  }
};</pre>

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

<pre class="crayon-plain-tag">class Solution:
  def countPairs(self, deliciousness: List[int]) -&gt; int:
    sums = [1&lt;&lt;i for i in range(22)]
    m = defaultdict(int)
    ans = 0
    for x in deliciousness:
      for t in sums:
        if t - x in m: ans += m[t - x]
      m[x] += 1
    return ans % (10**9 + 7)</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1711-count-good-meals/">花花酱 LeetCode 1711. Count Good Meals</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/hashtable/leetcode-1711-count-good-meals/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1498. Number of Subsequences That Satisfy the Given Sum Condition</title>
		<link>https://zxi.mytechroad.com/blog/two-pointers/leetcode-1498-number-of-subsequences-that-satisfy-the-given-sum-condition/</link>
					<comments>https://zxi.mytechroad.com/blog/two-pointers/leetcode-1498-number-of-subsequences-that-satisfy-the-given-sum-condition/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 28 Jun 2020 08:25:06 +0000</pubDate>
				<category><![CDATA[Two pointers]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[two pointers]]></category>
		<category><![CDATA[two sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6999</guid>

					<description><![CDATA[<p>Given an array of integers&#160;nums&#160;and an integer&#160;target. Return the number of&#160;non-empty&#160;subsequences of&#160;nums&#160;such that the sum of the minimum and maximum element on it is less&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-1498-number-of-subsequences-that-satisfy-the-given-sum-condition/">花花酱 LeetCode 1498. Number of Subsequences That Satisfy the Given Sum Condition</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 an array of integers&nbsp;<code>nums</code>&nbsp;and an integer&nbsp;<code>target</code>.</p>



<p>Return the number of&nbsp;<strong>non-empty</strong>&nbsp;subsequences of&nbsp;<code>nums</code>&nbsp;such that the sum of the minimum and maximum element on it is less or equal than&nbsp;<code>target</code>.</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>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,5,6,7], target = 9
<strong>Output:</strong> 4
<strong>Explanation: </strong>There are 4 subsequences that satisfy the condition.
[3] -&gt; Min value + max value &lt;= target (3 + 3 &lt;= 9)
[3,5] -&gt; (3 + 5 &lt;= 9)
[3,5,6] -&gt; (3 + 6 &lt;= 9)
[3,6] -&gt; (3 + 6 &lt;= 9)
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,3,6,8], target = 10
<strong>Output:</strong> 6
<strong>Explanation: </strong>There are 6 subsequences that satisfy the condition. (nums can have repeated numbers).
[3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6]</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,3,3,4,6,7], target = 12
<strong>Output:</strong> 61
<strong>Explanation: </strong>There are 63 non-empty subsequences, two of them don't satisfy the condition ([6,7], [7]).
Number of valid subsequences (63 - 2 = 61).
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [5,2,4,1,7,6,8], target = 16
<strong>Output:</strong> 127
<strong>Explanation: </strong>All non-empty subset satisfy the condition (2^7 - 1) = 127</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 10^5</code></li><li><code>1 &lt;= nums[i] &lt;= 10^6</code></li><li><code>1 &lt;= target &lt;= 10^6</code></li></ul>



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



<p>Since order of the elements in the subsequence doesn&#8217;t matter, we can sort the input array.<br>Very similar to two sum, we use two pointers (i, j) to maintain a window, s.t. nums[i] +nums[j] &lt;= target.<br>Then fix nums[i], any subset of (nums[i+1~j]) gives us a valid subsequence, thus we have 2^(j-(i+1)+1) = 2^(j-i) valid subsequence for window (i, j).</p>



<p>Time complexity: O(nlogn) // Sort<br>Space complexity: O(n) // need to precompute 2^n % kMod.</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int numSubseq(vector&lt;int&gt;&amp; nums, int target) {
    constexpr int kMod = 1e9 + 7;
    const int n = nums.size();
    vector&lt;int&gt; p(n + 1, 1);
    for (int i = 1; i &lt;= n; ++i) 
      p[i] = (p[i - 1] &lt;&lt; 1) % kMod;
    sort(begin(nums), end(nums));
    int ans = 0;
    for (int i = 0, j = n - 1; i &lt;= j; ++i) {
      while (i &lt;= j &amp;&amp; nums[i] + nums[j] &gt; target) --j;
      if (i &gt; j) continue;
      // In subarray nums[i~j]:
      // min = nums[i], max = nums[j]
      // nums[i] + nums[j] &lt;= target
      // {nums[i], (j - i - 1 + 1 values)}
      // Any subset of the right part gives a valid subsequence 
      // in the original array. And There are 2^(j - i) ones.
      ans = (ans + p[j - i]) % kMod;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-1498-number-of-subsequences-that-satisfy-the-given-sum-condition/">花花酱 LeetCode 1498. Number of Subsequences That Satisfy the Given Sum Condition</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-1498-number-of-subsequences-that-satisfy-the-given-sum-condition/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1013. Pairs of Songs With Total Durations Divisible by 60</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1013-pairs-of-songs-with-total-durations-divisible-by-60/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1013-pairs-of-songs-with-total-durations-divisible-by-60/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 Mar 2019 06:37:40 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[two sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4977</guid>

					<description><![CDATA[<p>In a list of songs, the&#160;i-th&#160;song has a duration of&#160;time[i]&#160;seconds.&#160; Return the number of pairs of songs for which their total&#160;duration in seconds is divisible&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1013-pairs-of-songs-with-total-durations-divisible-by-60/">花花酱 LeetCode 1013. Pairs of Songs With Total Durations Divisible by 60</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>In a list of songs, the&nbsp;<code>i</code>-th&nbsp;song has a duration of&nbsp;<code>time[i]</code>&nbsp;seconds.&nbsp;</p>



<p>Return the number of pairs of songs for which their total&nbsp;duration in seconds is divisible by&nbsp;<code>60</code>.&nbsp; Formally, we want the number of&nbsp;indices&nbsp;<code>i &lt; j</code>&nbsp;with&nbsp;<code>(time[i] + time[j]) % 60 == 0</code>.</p>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[30,20,150,100,40]
<strong>Output: </strong>3
<strong>Explanation: </strong>Three pairs have a total duration divisible by 60:
(time[0] = 30, time[2] = 150): total duration 180
(time[1] = 20, time[3] = 100): total duration 120
(time[1] = 20, time[4] = 40): total duration 60
</pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[60,60,60]
<strong>Output: </strong>3
<strong>Explanation: </strong>All three pairs have a total duration of 120, which is divisible by 60.
</pre>



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



<ol><li><code>1 &lt;= time.length &lt;= 60000</code></li><li><code>1 &lt;= time[i] &lt;= 500</code></li></ol>



<h2><strong>Solution: Two Sum of 60</strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua, running time: 36 ms, 11.6 MB
class Solution {
public:
  int numPairsDivisibleBy60(vector&lt;int&gt;&amp; time) {
    vector&lt;int&gt; c(60);
    int ans = 0;    
    for (int t : time) {
      t %= 60;
      ans += c[(60 - t) % 60];
      ++c[t];
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1013-pairs-of-songs-with-total-durations-divisible-by-60/">花花酱 LeetCode 1013. Pairs of Songs With Total Durations Divisible by 60</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/hashtable/leetcode-1013-pairs-of-songs-with-total-durations-divisible-by-60/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 167. Two Sum II &#8211; Input array is sorted</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-167-two-sum-ii-input-array-is-sorted/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-167-two-sum-ii-input-array-is-sorted/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 08 Jan 2018 03:01:12 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[Two pointers]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[two sum]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1532</guid>

					<description><![CDATA[<p>题目大意：给你一个排过序的数组，让你输出两个数的index，他们的和等于target。 Given an array of integers that is already&#160;sorted in ascending order, find two numbers such that they add up to a specific target number.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-167-two-sum-ii-input-array-is-sorted/">花花酱 LeetCode 167. Two Sum II &#8211; Input array is sorted</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="question-description">
<p>题目大意：给你一个排过序的数组，让你输出两个数的index，他们的和等于target。</p>
<p>Given an array of integers that is already&nbsp;<b><i>sorted in ascending order</i></b>, find two numbers such that they add up to a specific target number.</p>
<p>The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.</p>
<p>You may assume that each input would have&nbsp;<i>exactly</i>&nbsp;one solution and you may not use the&nbsp;<i>same</i>&nbsp;element twice.</p>
<p><b>Input:</b>&nbsp;numbers={2, 7, 11, 15}, target=9<br />
<b>Output:</b>&nbsp;index1=1, index2=2</p>
</div>
<p><strong>Solution:</strong></p>
<p>C++ / two pointers</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 6 ms
class Solution {
public:
    vector&lt;int&gt; twoSum(vector&lt;int&gt;&amp; numbers, int target) {
        int i = 0;
        int j = numbers.size() - 1;
        while (i &lt; j) {
            const int sum = numbers[i] + numbers[j];
            if (sum == target)
                break;
            else if (sum &lt; target)
                ++i;
            else
                --j;
        }
        return {i + 1, j + 1};
    }
};</pre><p>&nbsp;</p>
<p>C++ / Binary search</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 6 ms
class Solution {
public:
    vector&lt;int&gt; twoSum(vector&lt;int&gt;&amp; numbers, int target) {
        const int n = numbers.size();
        for (int i = 0; i &lt; n; ++i) {
            int l = i + 1;
            int r = n;
            int t = target - numbers[i];
            while (l &lt; r) {
                int m = l + (r - l) / 2;
                if (numbers[m] == t)
                    return {i + 1, m + 1};
                else if (numbers[m] &lt; t)
                    l = m + 1;
                else
                    r = m;
            }
        }
        return {};
    }
};</pre><p><strong>Related Problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/hashtable/leetcode-1-two-sum/">[解题报告] LeetCode 1. Two Sum</a></li>
<li><a href="http://zxi.mytechroad.com/blog/tree/leetcode-653-two-sum-iv-input-is-a-bst/">花花酱 LeetCode 653. Two Sum IV &#8211; Input is a BST</a></li>
</ul>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-167-two-sum-ii-input-array-is-sorted/">花花酱 LeetCode 167. Two Sum II &#8211; Input array is sorted</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/algorithms/binary-search/leetcode-167-two-sum-ii-input-array-is-sorted/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
