<?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>all pairs Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/all-pairs/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/all-pairs/</link>
	<description></description>
	<lastBuildDate>Sat, 05 Mar 2022 04:02:37 +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>all pairs Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/all-pairs/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2176. Count Equal and Divisible Pairs in an Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2176-count-equal-and-divisible-pairs-in-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2176-count-equal-and-divisible-pairs-in-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 05 Mar 2022 04:01:49 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[all pairs]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[Divisible]]></category>
		<category><![CDATA[easy]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9535</guid>

					<description><![CDATA[<p>Given a&#160;0-indexed&#160;integer array&#160;nums&#160;of length&#160;n&#160;and an integer&#160;k, return&#160;the&#160;number of pairs(i, j)where0 &#60;= i &#60; j &#60; n,&#160;such thatnums[i] == nums[j]and(i * j)is divisible byk. Example 1:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2176-count-equal-and-divisible-pairs-in-an-array/">花花酱 LeetCode 2176. Count Equal and Divisible 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>Given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>&nbsp;of length&nbsp;<code>n</code>&nbsp;and an integer&nbsp;<code>k</code>, return&nbsp;<em>the&nbsp;<strong>number of pairs</strong></em><code>(i, j)</code><em>where</em><code>0 &lt;= i &lt; j &lt; n</code>,&nbsp;<em>such that</em><code>nums[i] == nums[j]</code><em>and</em><code>(i * j)</code><em>is divisible by</em><code>k</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,1,2,2,2,1,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong>
There are 4 pairs that meet all the requirements:
- nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2.
- nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2.
- nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2.
- nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4], k = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 100</code></li><li><code>1 &lt;= nums[i], k &lt;= 100</code></li></ul>



<h2><strong>Solution: Brute Force</strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int countPairs(vector&lt;int&gt;&amp; nums, int k) {
    const int n = nums.size();
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        ans += (nums[i] == nums[j]) &amp;&amp; (i * j % k == 0);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2176-count-equal-and-divisible-pairs-in-an-array/">花花酱 LeetCode 2176. Count Equal and Divisible 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/algorithms/array/leetcode-2176-count-equal-and-divisible-pairs-in-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1782. Count Pairs Of Nodes</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-1782-count-pairs-of-nodes/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-1782-count-pairs-of-nodes/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 06 Mar 2021 18:30:54 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[all pairs]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[hard]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8203</guid>

					<description><![CDATA[<p>You are given an undirected graph represented by an integer&#160;n, which is the number of nodes, and&#160;edges, where&#160;edges[i] = [ui, vi]&#160;which indicates that there is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1782-count-pairs-of-nodes/">花花酱 LeetCode 1782. Count Pairs Of Nodes</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 undirected graph represented by an integer&nbsp;<code>n</code>, which is the number of nodes, and&nbsp;<code>edges</code>, where&nbsp;<code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>&nbsp;which indicates that there is an undirected edge between&nbsp;<code>u<sub>i</sub></code>&nbsp;and&nbsp;<code>v<sub>i</sub></code>. You are also given an integer array&nbsp;<code>queries</code>.</p>



<p>The answer to the&nbsp;<code>j<sup>th</sup></code>&nbsp;query is the number of pairs of nodes&nbsp;<code>(a, b)</code>&nbsp;that satisfy the following conditions:</p>



<ul><li><code>a &lt; b</code></li><li><code>cnt</code>&nbsp;is&nbsp;<strong>strictly greater</strong>&nbsp;than&nbsp;<code>queries[j]</code>, where&nbsp;<code>cnt</code>&nbsp;is the number of edges incident to&nbsp;<code>a</code>&nbsp;<strong>or</strong>&nbsp;<code>b</code>.</li></ul>



<p>Return an array&nbsp;<code>answers</code>&nbsp;such that&nbsp;<code>answers.length == queries.length</code>&nbsp;and&nbsp;<code>answers[j]</code>&nbsp;is the answer of the&nbsp;<code>j<sup>th</sup></code>&nbsp;query.</p>



<p>Note that there can be&nbsp;<strong>repeated edges</strong>.</p>



<p><strong>Example 1:</strong><img alt="" src="https://assets.leetcode.com/uploads/2021/02/11/screenshot-from-2021-02-11-23-07-35.png"></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, edges = [[1,2],[2,4],[1,3],[2,3],[2,1]], queries = [2,3]
<strong>Output:</strong> [6,5]
<strong>Explanation:</strong> The number of edges incident to at least one of each pair is shown above.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 5, edges = [[1,5],[1,5],[3,4],[2,5],[1,3],[5,1],[2,3],[2,5]], queries = [1,2,3,4,5]
<strong>Output:</strong> [10,10,9,8,6]
</pre>



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



<ul><li><code>2 &lt;= n &lt;= 2 * 10<sup>4</sup></code></li><li><code>1 &lt;= edges.length &lt;= 10<sup>5</sup></code></li><li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub>&nbsp;&lt;= n</code></li><li><code>u<sub>i&nbsp;</sub>!= v<sub>i</sub></code></li><li><code>1 &lt;= queries.length &lt;= 20</code></li><li><code>0 &lt;= queries[j] &lt; edges.length</code></li></ul>



<h2><strong>Solution 1: Pre-compute </strong></h2>



<p>Pre-compute # of pairs with total edges >= k. where k is from 0 to max_degree * 2 + 1.</p>



<p>Time complexity: (|node_degrees|<sup>2</sup> + V + E)<br>Space complexity: O(V+E)</p>



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

<pre class="crayon-plain-tag">// Author: huahua
class Solution {
public:
  vector&lt;int&gt; countPairs(int n, vector&lt;vector&lt;int&gt;&gt;&amp; edges, vector&lt;int&gt;&amp; queries) {
    vector&lt;int&gt; node_degrees(n);
    unordered_map&lt;int, int&gt; edge_freq;
    for (auto&amp; e : edges) {
      sort(begin(e), end(e));
      ++node_degrees[--e[0]];
      ++node_degrees[--e[1]];
      ++edge_freq[(e[0] &lt;&lt; 16) | e[1]];
    }
    
    const int max_degree = *max_element(begin(node_degrees), 
                                        end(node_degrees));
    // Need pad one more to handle &quot;not found / 0&quot; case.
    vector&lt;int&gt; counts(max_degree * 2 + 2);
    
    unordered_map&lt;int, int&gt; degree_count;
    for (int i = 0; i &lt; n; ++i) 
      ++degree_count[node_degrees[i]];
    
    for (auto&amp; [d1, c1] : degree_count)
      for (auto&amp; [d2, c2] : degree_count)
        // Only count once if degrees are different to ensure (a &lt; b)
        if (d1 &lt; d2) counts[d1 + d2] += c1 * c2;
        // If degrees are the same C(n, 2) to ensure (a &lt; b)
        else if (d1 == d2) counts[d1 * 2] += c1 * (c1 - 1) / 2;
    
    for (auto&amp; [key, freq] : edge_freq) {
      const int u = key &gt;&gt; 16;
      const int v = key &amp; 0xFFFF;
      // For a pair of (u, v) their actual edge count is 
      // d[u] + d[v] - freq[(u, v)] instead of d[u] + d[v]
      counts[node_degrees[u] + node_degrees[v]] -= 1;
      counts[node_degrees[u] + node_degrees[v] - freq] += 1;
    }
    
    // counts[i] = # of pairs whose total edges &gt;= i
    for (int i = counts.size() - 2; i &gt;= 0; --i)
      counts[i] += counts[i + 1];
    
    vector&lt;int&gt; ans;
    for (int q : queries)
      ans.push_back(counts[min(q + 1, static_cast&lt;int&gt;(counts.size() - 1))]);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1782-count-pairs-of-nodes/">花花酱 LeetCode 1782. Count Pairs Of Nodes</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/graph/leetcode-1782-count-pairs-of-nodes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1615. Maximal Network Rank</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-1615-maximal-network-rank/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-1615-maximal-network-rank/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 11 Oct 2020 05:14:11 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[all pairs]]></category>
		<category><![CDATA[degrees]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7477</guid>

					<description><![CDATA[<p>There is an infrastructure of&#160;n&#160;cities with some number of&#160;roads&#160;connecting these cities. Each&#160;roads[i] = [ai, bi]&#160;indicates that there is a bidirectional road between cities&#160;ai&#160;and&#160;bi. The&#160;network rankof&#160;two&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1615-maximal-network-rank/">花花酱 LeetCode 1615. Maximal Network Rank</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>There is an infrastructure of&nbsp;<code>n</code>&nbsp;cities with some number of&nbsp;<code>roads</code>&nbsp;connecting these cities. Each&nbsp;<code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>&nbsp;indicates that there is a bidirectional road between cities&nbsp;<code>a<sub>i</sub></code>&nbsp;and&nbsp;<code>b<sub>i</sub></code>.</p>



<p>The&nbsp;<strong>network rank</strong>of&nbsp;<strong>two different cities</strong>&nbsp;is defined as the total number of&nbsp;<strong>directly</strong>&nbsp;connected roads to&nbsp;<strong>either</strong>&nbsp;city. If a road is directly connected to both cities, it is only counted&nbsp;<strong>once</strong>.</p>



<p>The&nbsp;<strong>maximal network rank&nbsp;</strong>of the infrastructure is the&nbsp;<strong>maximum network rank</strong>&nbsp;of all pairs of different cities.</p>



<p>Given the integer&nbsp;<code>n</code>&nbsp;and the array&nbsp;<code>roads</code>, return&nbsp;<em>the&nbsp;<strong>maximal network rank</strong>&nbsp;of the entire infrastructure</em>.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/09/21/ex1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, roads = [[0,1],[0,3],[1,2],[1,3]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The network rank of cities 0 and 1 is 4 as there are 4 roads that are connected to either 0 or 1. The road between 0 and 1 is only counted once.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/09/21/ex2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 5, roads = [[0,1],[0,3],[1,2],[1,3],[2,3],[2,4]]
<strong>Output:</strong> 5
<strong>Explanation:</strong> There are 5 roads that are connected to cities 1 or 2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 8, roads = [[0,1],[1,2],[2,3],[2,4],[5,6],[5,7]]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The network rank of 2 and 5 is 5. Notice that all the cities do not have to be connected.
</pre>



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



<ul><li><code>2 &lt;= n &lt;= 100</code></li><li><code>0 &lt;= roads.length &lt;= n * (n - 1) / 2</code></li><li><code>roads[i].length == 2</code></li><li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub>&nbsp;&lt;= n-1</code></li><li><code>a<sub>i</sub>&nbsp;!=&nbsp;b<sub>i</sub></code></li><li>Each&nbsp;pair of cities has&nbsp;<strong>at most one</strong>&nbsp;road connecting them.</li></ul>



<h2><strong>Solution: Counting degrees and all pairs</strong></h2>



<p>Counting degrees for each node, if a and b are not connected, ans = degrees(a) + degrees(b), otherwise ans -= 1</p>



<p>Time complexity: O(E + V^2)<br>Space complexity: O(E)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maximalNetworkRank(int n, vector&lt;vector&lt;int&gt;&gt;&amp; roads) {
    vector&lt;int&gt; degrees(n);
    unordered_set&lt;int&gt; connected;
    for (const auto&amp; road : roads) {
      ++degrees[road[0]];
      ++degrees[road[1]];
      connected.insert((road[0] &lt;&lt; 16) | road[1]);
      connected.insert((road[1] &lt;&lt; 16) | road[0]);
    }
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        ans = max(ans, degrees[i] + degrees[j] 
                         - (connected.count((i &lt;&lt; 16) | j) ? 1 : 0));
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1615-maximal-network-rank/">花花酱 LeetCode 1615. Maximal Network Rank</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/graph/leetcode-1615-maximal-network-rank/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1200. Minimum Absolute Difference</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-1200-minimum-absolute-difference/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-1200-minimum-absolute-difference/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 22 Sep 2019 08:39:20 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[all pairs]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[O(nlogn)]]></category>
		<category><![CDATA[sorting]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5569</guid>

					<description><![CDATA[<p>Given an&#160;array&#160;of&#160;distinct&#160;integers&#160;arr, find all pairs of elements with the minimum absolute difference of any two elements.&#160; Return a list of pairs in ascending order(with respect&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1200-minimum-absolute-difference/">花花酱 LeetCode 1200. Minimum Absolute Difference</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&nbsp;array&nbsp;of&nbsp;<strong>distinct</strong>&nbsp;integers&nbsp;<code>arr</code>, find all pairs of elements with the minimum absolute difference of any two elements.&nbsp;</p>



<p>Return a list of pairs in ascending order(with respect to pairs), each pair&nbsp;<code>[a, b]</code>&nbsp;follows</p>



<ul><li><code>a, b</code>&nbsp;are from&nbsp;<code>arr</code></li><li><code>a &lt; b</code></li><li><code>b - a</code>&nbsp;equals to the minimum absolute difference of any two elements in&nbsp;<code>arr</code></li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [4,2,1,3]
<strong>Output:</strong> [[1,2],[2,3],[3,4]]
<strong>Explanation: </strong>The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,3,6,10,15]
<strong>Output:</strong> [[1,3]]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [3,8,-10,23,19,-4,-14,27]
<strong>Output:</strong> [[-14,-10],[19,23],[23,27]]
</pre>



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



<ul><li><code>2 &lt;= arr.length &lt;= 10^5</code></li><li><code>-10^6 &lt;= arr[i] &lt;= 10^6</code></li></ul>



<h2><strong>Solution: Sorting</strong></h2>



<p>The min abs difference could only happen between consecutive numbers in sorted form.</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; minimumAbsDifference(vector&lt;int&gt;&amp; arr) {
    sort(begin(arr), end(arr));
    vector&lt;vector&lt;int&gt;&gt; ans;
    int best = INT_MAX;
    for (int i = 1; i &lt; arr.size(); ++i) {
      int d = abs(arr[i] - arr[i - 1]);      
      if (d &lt; best) {
        ans.clear();
        best = d;
      }
      if (d == best) ans.push_back({arr[i - 1], arr[i]});      
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1200-minimum-absolute-difference/">花花酱 LeetCode 1200. Minimum Absolute Difference</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/greedy/leetcode-1200-minimum-absolute-difference/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 962. Maximum Width Ramp</title>
		<link>https://zxi.mytechroad.com/blog/stack/leetcode-962-maximum-width-ramp/</link>
					<comments>https://zxi.mytechroad.com/blog/stack/leetcode-962-maximum-width-ramp/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 26 Dec 2018 07:30:42 +0000</pubDate>
				<category><![CDATA[Stack]]></category>
		<category><![CDATA[all pairs]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[decreasing]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[stack]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4537</guid>

					<description><![CDATA[<p>Given an array&#160;A&#160;of integers, a&#160;ramp&#160;is a tuple&#160;(i, j)&#160;for which&#160;i &#60; j&#160;and&#160;A[i] &#60;= A[j].&#160; The width of such a&#160;ramp is&#160;j - i. Find the maximum width&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/stack/leetcode-962-maximum-width-ramp/">花花酱 LeetCode 962. Maximum Width Ramp</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>A</code>&nbsp;of integers, a&nbsp;<em>ramp</em>&nbsp;is a tuple&nbsp;<code>(i, j)</code>&nbsp;for which&nbsp;<code>i &lt; j</code>&nbsp;and&nbsp;<code>A[i] &lt;= A[j]</code>.&nbsp; The width of such a&nbsp;ramp is&nbsp;<code>j - i</code>.</p>



<p>Find the maximum width of a ramp in&nbsp;<code>A</code>.&nbsp; If one doesn&#8217;t exist, return 0.</p>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[6,0,8,2,1,5] <br><strong>Output: </strong>4 <br><strong>Explanation: </strong> The maximum width ramp is achieved at (i, j) = (1, 5): A[1] = 0 and A[5] = 5. </pre>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[9,8,1,0,1,9,4,0,4,1] <br><strong>Output: </strong>7 <br><strong>Explanation: </strong> The maximum width ramp is achieved at (i, j) = (2, 9): A[2] = 1 and A[9] = 1. </pre>



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



<ol><li><code>2 &lt;= A.length &lt;= 50000</code></li><li><code>0 &lt;= A[i] &lt;= 50000</code></li></ol>



<h1><strong>Solution: Stack</strong></h1>



<ol><li>Using a stack to store start candidates&#8217; (decreasing order) index</li><li>Scan from right to left, compare the current number with the one on the top of the stack, pop if greater.</li></ol>



<p>e.g. <br>A = [<strong>6</strong>,<strong>0</strong>,8,2,1,5]<br>stack = [0, 1] => [6, 0] <br>cur: A[5] = 5, stack.top = A[1] = 0, ramp = 5, stack.pop()<br>cur: A[4] = 1, stack.top = A[0] = 6<br>cur: A[3] = 2, stack.top = A[0] = 6<br>cur: A[2] = 8,  stack.top = A[0] = 6, ramp = 2, stack.pop()<br>stack.isEmpty() => END</p>



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

<pre class="crayon-plain-tag">// Author: Huahua, running time: 44 ms
class Solution {
public:
  int maxWidthRamp(vector&lt;int&gt;&amp; A) {
    stack&lt;int&gt; s;
    for (int i = 0; i &lt; A.size(); ++i)
      if (s.empty() || A[i] &lt; A[s.top()])
        s.push(i);
    int ans = 0;
    for (int i = A.size() - 1; i &gt;= 0; --i)
      while (!s.empty() &amp;&amp; A[i] &gt;= A[s.top()]) {
        ans = max(ans, i - s.top());
        s.pop();
      }
    return ans;
  }
};</pre>

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

<pre class="crayon-plain-tag"># Author: Huahua, running time: 92 ms
class Solution:
  def maxWidthRamp(self, A):
    s = []
    for i in range(len(A)):
      if not s or A[i] &lt; A[s[-1]]: s.append(i)
    ans = 0
    for i in range(len(A) - 1, -1, -1):
      while s and A[i] &gt;= A[s[-1]]:
        ans = max(ans, i - s.pop())        
    return ans</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/stack/leetcode-962-maximum-width-ramp/">花花酱 LeetCode 962. Maximum Width Ramp</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/stack/leetcode-962-maximum-width-ramp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 908. Smallest Range I</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-908-smallest-range-i/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-908-smallest-range-i/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 24 Sep 2018 06:02:15 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[all pairs]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[range]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4067</guid>

					<description><![CDATA[<p>Problem Given an array A of integers, for each integer A[i] we may choose any x with -K &#60;= x &#60;= K, and add xto A[i]. After this process, we have some array B. Return&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-908-smallest-range-i/">花花酱 LeetCode 908. Smallest Range I</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given an array <code>A</code> of integers, for each integer <code>A[i]</code> we may choose any <code>x</code> with <code>-K &lt;= x &lt;= K</code>, and add <code>x</code>to <code>A[i]</code>.</p>
<p>After this process, we have some array <code>B</code>.</p>
<p>Return the smallest possible difference between the maximum value of <code>B</code> and the minimum value of <code>B</code>.</p>
<div>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>A = <span id="example-input-1-1">[1]</span>, K = <span id="example-input-1-2">0</span>
<strong>Output: </strong><span id="example-output-1">0
<strong>Explanation</strong>: B = [1]</span>
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>A = <span id="example-input-2-1">[0,10]</span>, K = <span id="example-input-2-2">2</span>
<strong>Output: </strong><span id="example-output-2">6
</span><span id="example-output-1"><strong>Explanation</strong>: B = [2,8]</span>
</pre>
<div>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false "><strong>Input: </strong>A = <span id="example-input-3-1">[1,3,6]</span>, K = <span id="example-input-3-2">3</span>
<strong>Output: </strong><span id="example-output-3">0
</span><span id="example-output-1"><strong>Explanation</strong>: B = [3,3,3] or B = [4,4,4]</span>
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= A.length &lt;= 10000</code></li>
<li><code>0 &lt;= A[i] &lt;= 10000</code></li>
<li><code>0 &lt;= K &lt;= 10000</code></li>
</ol>
<h1>Solution 0: Brute Force (TLE)</h1>
<p>Try all pairs</p>
<p>Time complexity: O(n^2)</p>
<p>Space complexity: O(1)</p>
<h1>Solution 1: Math</h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>Find the min/max element of the array.</p>
<p>min + k v.s. max &#8211; k</p>
<p>ans = max(0, (max &#8211; min) &#8211; 2 * k))</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int smallestRangeI(vector&lt;int&gt;&amp; A, int K) {
    int a_min = *min_element(begin(A), end(A));
    int a_max = *max_element(begin(A), end(A));
    return max(0, (a_max - a_min) - 2 * K);
  }
};</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag"># Author: Huahua, 72 ms
class Solution:
  def smallestRangeI(self, A, K):
    return max(0, max(A) - min(A) - 2 * K);</pre><p></div></div></p>
</div>
</div>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-908-smallest-range-i/">花花酱 LeetCode 908. Smallest Range I</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/math/leetcode-908-smallest-range-i/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
