<?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>brute force Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/brute-force/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/brute-force/</link>
	<description></description>
	<lastBuildDate>Sat, 29 Apr 2023 01:37:31 +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>brute force Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/brute-force/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2644. Find the Maximum Divisibility Score</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2644-find-the-maximum-divisibility-score/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2644-find-the-maximum-divisibility-score/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 29 Apr 2023 01:36:56 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[divisor]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10003</guid>

					<description><![CDATA[<p>You are given two&#160;0-indexed&#160;integer arrays&#160;nums&#160;and&#160;divisors. The&#160;divisibility score&#160;of&#160;divisors[i]&#160;is the number of indices&#160;j&#160;such that&#160;nums[j]&#160;is divisible by&#160;divisors[i]. Return&#160;the integer&#160;divisors[i]&#160;with the maximum divisibility score. If there is more than&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2644-find-the-maximum-divisibility-score/">花花酱 LeetCode 2644. Find the Maximum Divisibility 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>0-indexed</strong>&nbsp;integer arrays&nbsp;<code>nums</code>&nbsp;and&nbsp;<code>divisors</code>.</p>



<p>The&nbsp;<strong>divisibility score</strong>&nbsp;of&nbsp;<code>divisors[i]</code>&nbsp;is the number of indices&nbsp;<code>j</code>&nbsp;such that&nbsp;<code>nums[j]</code>&nbsp;is divisible by&nbsp;<code>divisors[i]</code>.</p>



<p>Return&nbsp;<em>the integer</em>&nbsp;<code>divisors[i]</code>&nbsp;<em>with the maximum divisibility score</em>. If there is more than one integer with the maximum score, return the minimum of them.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [4,7,9,3,9], divisors = [5,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The divisibility score for every element in divisors is:
The divisibility score of divisors[0] is 0 since no number in nums is divisible by 5.
The divisibility score of divisors[1] is 1 since nums[0] is divisible by 2.
The divisibility score of divisors[2] is 3 since nums[2], nums[3], and nums[4] are divisible by 3.
Since divisors[2] has the maximum divisibility score, we return it.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [20,14,21,10], divisors = [5,7,5]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The divisibility score for every element in divisors is:
The divisibility score of divisors[0] is 2 since nums[0] and nums[3] are divisible by 5.
The divisibility score of divisors[1] is 2 since nums[1] and nums[2] are divisible by 7.
The divisibility score of divisors[2] is 2 since nums[0] and nums[3] are divisible by 5.
Since divisors[0], divisors[1], and divisors[2] all have the maximum divisibility score, we return the minimum of them (i.e., divisors[2]).
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [12], divisors = [10,16]
<strong>Output:</strong> 10
<strong>Explanation:</strong> The divisibility score for every element in divisors is:
The divisibility score of divisors[0] is 0 since no number in nums is divisible by 10.
The divisibility score of divisors[1] is 0 since no number in nums is divisible by 16.
Since divisors[0] and divisors[1] both have the maximum divisibility score, we return the minimum of them (i.e., divisors[0]).
</pre>



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



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



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



<p>Time complexity: O(m*n)<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 maxDivScore(vector&lt;int&gt;&amp; nums, vector&lt;int&gt;&amp; divisors) {
    int maxScore = 0;
    int ans = INT_MAX;
    for (int d : divisors) {
      int score = 0;
      for (int x : nums) {
        score += x % d == 0;
      }
      if (score &gt; maxScore) {
        maxScore = score;
        ans = d;
      }
      if (score == maxScore)
        ans = min(ans, d);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2644-find-the-maximum-divisibility-score/">花花酱 LeetCode 2644. Find the Maximum Divisibility 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/algorithms/array/leetcode-2644-find-the-maximum-divisibility-score/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2249. Count Lattice Points Inside a Circle</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-2249-count-lattice-points-inside-a-circle/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-2249-count-lattice-points-inside-a-circle/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 27 Apr 2022 15:57:22 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9688</guid>

					<description><![CDATA[<p>Given a 2D integer array&#160;circles&#160;where&#160;circles[i] = [xi, yi, ri]&#160;represents the center&#160;(xi, yi)&#160;and radius&#160;ri&#160;of the&#160;ith&#160;circle drawn on a grid, return&#160;the&#160;number of lattice points&#160;that are present inside&#160;at&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-2249-count-lattice-points-inside-a-circle/">花花酱 LeetCode 2249. Count Lattice Points Inside a Circle</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 2D integer array&nbsp;<code>circles</code>&nbsp;where&nbsp;<code>circles[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code>&nbsp;represents the center&nbsp;<code>(x<sub>i</sub>, y<sub>i</sub>)</code>&nbsp;and radius&nbsp;<code>r<sub>i</sub></code>&nbsp;of the&nbsp;<code>i<sup>th</sup></code>&nbsp;circle drawn on a grid, return&nbsp;<em>the&nbsp;<strong>number of lattice points</strong>&nbsp;</em><em>that are present inside&nbsp;<strong>at least one</strong>&nbsp;circle</em>.</p>



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



<ul><li>A&nbsp;<strong>lattice point</strong>&nbsp;is a point with integer coordinates.</li><li>Points that lie&nbsp;<strong>on the circumference of a circle</strong>&nbsp;are also considered to be inside it.</li></ul>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/03/02/exa-11.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> circles = [[2,2,1]]
<strong>Output:</strong> 5
<strong>Explanation:</strong>
The figure above shows the given circle.
The lattice points present inside the circle are (1, 2), (2, 1), (2, 2), (2, 3), and (3, 2) and are shown in green.
Other points such as (1, 1) and (1, 3), which are shown in red, are not considered inside the circle.
Hence, the number of lattice points present inside at least one circle is 5.</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/03/02/exa-22.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> circles = [[2,2,2],[3,4,1]]
<strong>Output:</strong> 16
<strong>Explanation:</strong>
The figure above shows the given circles.
There are exactly 16 lattice points which are present inside at least one circle. 
Some of them are (0, 2), (2, 0), (2, 4), (3, 2), and (4, 4).
</pre>



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



<ul><li><code>1 &lt;= circles.length &lt;= 200</code></li><li><code>circles[i].length == 3</code></li><li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub>&nbsp;&lt;= 100</code></li><li><code>1 &lt;= r<sub>i</sub>&nbsp;&lt;= min(x<sub>i</sub>, y<sub>i</sub>)</code></li></ul>



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



<p>Time complexity: O(m * m * n) = O(200 * 200 * 200)<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 countLatticePoints(vector&lt;vector&lt;int&gt;&gt;&amp; circles) {
    auto isIn = [](int u, int v, int x, int y, int r) {
      return (u - x) * (u - x) + (v - y) * (v - y) &lt;= r * r;
    };    
    int ans = 0;
    for (int u = 0; u &lt;= 200; ++u)
      for (int v = 0; v &lt;= 200; ++v) {
        bool found = false;
        for (const auto&amp; c : circles)
          if (isIn(u, v, c[0], c[1], c[2])) {
            found = true;
            break;
          }
        ans += found;
      }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-2249-count-lattice-points-inside-a-circle/">花花酱 LeetCode 2249. Count Lattice Points Inside a Circle</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/geometry/leetcode-2249-count-lattice-points-inside-a-circle/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 1620. Coordinate With Maximum Network Quality</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-1620-coordinate-with-maximum-network-quality/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-1620-coordinate-with-maximum-network-quality/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 17 Oct 2020 20:29:03 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[gemoetry]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7516</guid>

					<description><![CDATA[<p>You are given an array of network towers&#160;towers&#160;and an integer&#160;radius, where&#160;towers[i] = [xi, yi, qi]&#160;denotes the&#160;ith&#160;network tower with location&#160;(xi, yi)&#160;and quality factor&#160;qi. All the coordinates&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1620-coordinate-with-maximum-network-quality/">花花酱 LeetCode 1620. Coordinate With Maximum Network Quality</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 of network towers&nbsp;<code>towers</code>&nbsp;and an integer&nbsp;<code>radius</code>, where&nbsp;<code>towers[i] = [x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>]</code>&nbsp;denotes the&nbsp;<code>i<sup>th</sup></code>&nbsp;network tower with location&nbsp;<code>(x<sub>i</sub>, y<sub>i</sub>)</code>&nbsp;and quality factor&nbsp;<code>q<sub>i</sub></code>. All the coordinates are&nbsp;<strong>integral coordinates</strong>&nbsp;on the X-Y plane, and the distance between two coordinates is the&nbsp;<strong>Euclidean distance</strong>.</p>



<p>The integer&nbsp;<code>radius</code>&nbsp;denotes the&nbsp;<strong>maximum distance</strong>&nbsp;in which the tower is&nbsp;<strong>reachable</strong>. The tower is&nbsp;<strong>reachable</strong>&nbsp;if the distance is less than or equal to&nbsp;<code>radius</code>. Outside that distance, the signal becomes garbled, and the tower is&nbsp;<strong>not reachable</strong>.</p>



<p>The signal quality of the&nbsp;<code>i<sup>th</sup></code>&nbsp;tower at a coordinate&nbsp;<code>(x, y)</code>&nbsp;is calculated with the formula&nbsp;<code>⌊q<sub>i</sub>&nbsp;/ (1 + d)⌋</code>, where&nbsp;<code>d</code>&nbsp;is the distance between the tower and the coordinate. The&nbsp;<strong>network quality</strong>&nbsp;at a coordinate is the sum of the signal qualities from all the&nbsp;<strong>reachable</strong>&nbsp;towers.</p>



<p>Return&nbsp;<em>the integral coordinate where the&nbsp;<strong>network quality</strong>&nbsp;is maximum</em>. If there are multiple coordinates with the same&nbsp;<strong>network quality</strong>, return&nbsp;<em>the lexicographically minimum coordinate</em>.</p>



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



<ul><li>A coordinate&nbsp;<code>(x1, y1)</code>&nbsp;is lexicographically smaller than&nbsp;<code>(x2, y2)</code>&nbsp;if either&nbsp;<code>x1 &lt; x2</code>&nbsp;or&nbsp;<code>x1 == x2</code>&nbsp;and&nbsp;<code>y1 &lt; y2</code>.</li><li><code>⌊val⌋</code>&nbsp;is the greatest integer less than or equal to&nbsp;<code>val</code>&nbsp;(the floor function).</li></ul>



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



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> towers = [[1,2,5],[2,1,7],[3,1,9]], radius = 2
<strong>Output:</strong> [2,1]
<strong>Explanation: </strong>
At coordinate (2, 1) the total quality is 13
- Quality of 7 from (2, 1) results in ⌊7 / (1 + sqrt(0)⌋ = ⌊7⌋ = 7
- Quality of 5 from (1, 2) results in ⌊5 / (1 + sqrt(2)⌋ = ⌊2.07⌋ = 2
- Quality of 9 from (3, 1) results in ⌊9 / (1 + sqrt(1)⌋ = ⌊4.5⌋ = 4
No other coordinate has higher quality.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> towers = [[23,11,21]], radius = 9
<strong>Output:</strong> [23,11]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> towers = [[1,2,13],[2,1,7],[0,1,9]], radius = 2
<strong>Output:</strong> [1,2]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> towers = [[2,1,9],[0,1,9]], radius = 2
<strong>Output:</strong> [0,1]
<strong>Explanation: </strong>Both (0, 1) and (2, 1) are optimal in terms of quality but (0, 1) is lexicograpically minimal.
</pre>



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



<ul><li><code>1 &lt;= towers.length &lt;= 50</code></li><li><code>towers[i].length == 3</code></li><li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>&nbsp;&lt;= 50</code></li><li><code>1 &lt;= radius &lt;= 50</code></li></ul>



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



<p>Try all possible coordinates from (0, 0) to (50, 50).</p>



<p>Time complexity: O(|X|*|Y|*t)<br>Space complexity: O(1)</p>



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

<pre class="crayon-plain-tag">class Solution {
public:
  vector&lt;int&gt; bestCoordinate(vector&lt;vector&lt;int&gt;&gt;&amp; towers, int radius) {
    constexpr int n = 50;
    vector&lt;int&gt; ans;
    int max_q = 0;    
    for (int x = 0; x &lt;= n; ++x)
      for (int y = 0; y &lt;= n; ++y) {
        int q = 0;
        for (const auto&amp; tower : towers) {
          const int tx = tower[0], ty = tower[1];
          const float d = sqrt((x - tx) * (x - tx) + (y - ty) * (y - ty));
          if (d &gt; radius) continue;
          q += floor(tower[2] / (1 + d));
        }
        if (q &gt; max_q) {
          max_q = q;
          ans = {x, y};
        }
      }    
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-1620-coordinate-with-maximum-network-quality/">花花酱 LeetCode 1620. Coordinate With Maximum Network Quality</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/geometry/leetcode-1620-coordinate-with-maximum-network-quality/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1608. Special Array With X Elements Greater Than or Equal X</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1608-special-array-with-x-elements-greater-than-or-equal-x/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1608-special-array-with-x-elements-greater-than-or-equal-x/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 04 Oct 2020 17:33:56 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[easy]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7456</guid>

					<description><![CDATA[<p>You are given an array&#160;nums&#160;of non-negative integers.&#160;nums&#160;is considered&#160;special&#160;if there exists a number&#160;x&#160;such that there are&#160;exactly&#160;x&#160;numbers in&#160;nums&#160;that are&#160;greater than or equal to&#160;x. Notice that&#160;x&#160;does not&#160;have to&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1608-special-array-with-x-elements-greater-than-or-equal-x/">花花酱 LeetCode 1608. Special Array With X Elements Greater Than or Equal X</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;of non-negative integers.&nbsp;<code>nums</code>&nbsp;is considered&nbsp;<strong>special</strong>&nbsp;if there exists a number&nbsp;<code>x</code>&nbsp;such that there are&nbsp;<strong>exactly</strong>&nbsp;<code>x</code>&nbsp;numbers in&nbsp;<code>nums</code>&nbsp;that are&nbsp;<strong>greater than or equal to</strong>&nbsp;<code>x</code>.</p>



<p>Notice that&nbsp;<code>x</code>&nbsp;<strong>does not</strong>&nbsp;have to be an element in&nbsp;<code>nums</code>.</p>



<p>Return&nbsp;<code>x</code>&nbsp;<em>if the array is&nbsp;<strong>special</strong>, otherwise, return&nbsp;</em><code>-1</code>. It can be proven that if&nbsp;<code>nums</code>&nbsp;is special, the value for&nbsp;<code>x</code>&nbsp;is&nbsp;<strong>unique</strong>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,5]
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are 2 values (3 and 5) that are greater than or equal to 2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [0,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong> No numbers fit the criteria for x.
If x = 0, there should be 0 numbers &gt;= x, but there are 2.
If x = 1, there should be 1 number &gt;= x, but there are 0.
If x = 2, there should be 2 numbers &gt;= x, but there are 0.
x cannot be greater since there are only 2 numbers in nums.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [0,4,3,0,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are 3 values that are greater than or equal to 3.
</pre>



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



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



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



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



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



<p>Try all possible x from 0 to n.</p>



<p>Time complexity: O(n^2)<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 specialArray(vector&lt;int&gt;&amp; nums) {
    for (int x = 0; x &lt;= nums.size(); ++x)
      if (x == count_if(begin(nums), end(nums), [x](int num) { 
            return num &gt;= x; }))
        return x;
    return -1;    
  }
};</pre>

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

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def specialArray(self, nums: List[int]) -&gt; int:
    for x in range(len(nums) + 1):
      if x == sum([n &gt;= x for n in nums]): return x
    return -1</pre>
</div></div>



<h2><strong>Solution 2: Counting Sort</strong></h2>



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



<p>f[i] := sum(nums >= i)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int specialArray(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    vector&lt;int&gt; f(n + 2);
    for (int v : nums) ++f[min(v, n)];
    for (int i = n; i &gt;= 0; --i)
      if ((f[i] += f[i + 1]) == i) return i;
    return -1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1608-special-array-with-x-elements-greater-than-or-equal-x/">花花酱 LeetCode 1608. Special Array With X Elements Greater Than or Equal X</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-1608-special-array-with-x-elements-greater-than-or-equal-x/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1534. Count Good Triplets</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1534-count-good-triplets/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1534-count-good-triplets/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 02 Aug 2020 04:15:38 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[easy]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7188</guid>

					<description><![CDATA[<p>Given an array of integers&#160;arr, and three integers&#160;a,&#160;b&#160;and&#160;c. You need to find the number of good triplets. A triplet&#160;(arr[i], arr[j], arr[k])&#160;is&#160;good&#160;if the following conditions are&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1534-count-good-triplets/">花花酱 LeetCode 1534. Count Good Triplets</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>arr</code>, and three integers&nbsp;<code>a</code>,&nbsp;<code>b</code>&nbsp;and&nbsp;<code>c</code>. You need to find the number of good triplets.</p>



<p>A triplet&nbsp;<code>(arr[i], arr[j], arr[k])</code>&nbsp;is&nbsp;<strong>good</strong>&nbsp;if the following conditions are true:</p>



<ul><li><code>0 &lt;= i &lt; j &lt; k &lt;&nbsp;arr.length</code></li><li><code>|arr[i] - arr[j]| &lt;= a</code></li><li><code>|arr[j] - arr[k]| &lt;= b</code></li><li><code>|arr[i] - arr[k]| &lt;= c</code></li></ul>



<p>Where&nbsp;<code>|x|</code>&nbsp;denotes the absolute value of&nbsp;<code>x</code>.</p>



<p>Return<em>&nbsp;the number of good triplets</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong>&nbsp;There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)].
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr = [1,1,2,2,3], a = 0, b = 0, c = 1
<strong>Output:</strong> 0
<strong>Explanation: </strong>No triplet satisfies all conditions.
</pre>



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



<ul><li><code>3 &lt;= arr.length &lt;= 100</code></li><li><code>0 &lt;= arr[i] &lt;= 1000</code></li><li><code>0 &lt;= a, b, c &lt;= 1000</code></li></ul>



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



<p>Time complexity: O(n^3)<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 countGoodTriplets(vector&lt;int&gt;&amp; arr, int a, int b, int c) {
    const int n = arr.size();
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        for (int k = j + 1; k &lt; n; ++k)
          if (abs(arr[i] - arr[j]) &lt;= a &amp;&amp;
              abs(arr[j] - arr[k]) &lt;= b &amp;&amp;
              abs(arr[i] - arr[k]) &lt;= c)
            ++ans;
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1534-count-good-triplets/">花花酱 LeetCode 1534. Count Good Triplets</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-1534-count-good-triplets/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1450. Number of Students Doing Homework at a Given Time</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1450-number-of-students-doing-homework-at-a-given-time/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1450-number-of-students-doing-homework-at-a-given-time/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 May 2020 04:11:55 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[interval]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6761</guid>

					<description><![CDATA[<p>Given two integer arrays&#160;startTime&#160;and&#160;endTime&#160;and given an integer&#160;queryTime. The&#160;ith&#160;student started doing their homework at the time&#160;startTime[i]&#160;and finished it at time&#160;endTime[i]. Return&#160;the number of students&#160;doing their homework&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1450-number-of-students-doing-homework-at-a-given-time/">花花酱 LeetCode 1450. Number of Students Doing Homework at a Given Time</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 integer arrays&nbsp;<code>startTime</code>&nbsp;and&nbsp;<code>endTime</code>&nbsp;and given an integer&nbsp;<code>queryTime</code>.</p>



<p>The&nbsp;<code>ith</code>&nbsp;student started doing their homework at the time&nbsp;<code>startTime[i]</code>&nbsp;and finished it at time&nbsp;<code>endTime[i]</code>.</p>



<p>Return&nbsp;<em>the number of students</em>&nbsp;doing their homework at time&nbsp;<code>queryTime</code>. More formally, return the number of students where&nbsp;<code>queryTime</code>&nbsp;lays in the interval&nbsp;<code>[startTime[i], endTime[i]]</code>&nbsp;inclusive.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> startTime = [1,2,3], endTime = [3,2,7], queryTime = 4
<strong>Output:</strong> 1
<strong>Explanation:</strong> We have 3 students where:
The first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4.
The second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4.
The third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> startTime = [4], endTime = [4], queryTime = 4
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only student was doing their homework at the queryTime.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> startTime = [4], endTime = [4], queryTime = 5
<strong>Output:</strong> 0
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> startTime = [1,1,1,1], endTime = [1,3,2,4], queryTime = 7
<strong>Output:</strong> 0
</pre>



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



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



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



<ul><li><code>startTime.length == endTime.length</code></li><li><code>1 &lt;= startTime.length &lt;= 100</code></li><li><code>1 &lt;= startTime[i] &lt;= endTime[i] &lt;= 1000</code></li><li><code>1 &lt;=&nbsp;queryTime &lt;= 1000</code></li></ul>



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



<p>Time complexity: O(n)<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 busyStudent(vector&lt;int&gt;&amp; startTime, vector&lt;int&gt;&amp; endTime, int queryTime) {
    int ans = 0;
    for (int i = 0; i &lt; startTime.size(); ++i)
      if (startTime[i] &lt;= queryTime &amp;&amp; queryTime &lt;= endTime[i]) ++ans;
    return ans;
  }
};</pre>
</div></div>



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

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -&gt; int:
    return sum(s &lt;= queryTime &lt;= e for s, e in zip(startTime, endTime))</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1450-number-of-students-doing-homework-at-a-given-time/">花花酱 LeetCode 1450. Number of Students Doing Homework at a Given Time</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-1450-number-of-students-doing-homework-at-a-given-time/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1385. Find the Distance Value Between Two Arrays</title>
		<link>https://zxi.mytechroad.com/blog/two-pointers/leetcode-1385-find-the-distance-value-between-two-arrays/</link>
					<comments>https://zxi.mytechroad.com/blog/two-pointers/leetcode-1385-find-the-distance-value-between-two-arrays/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 21 Mar 2020 23:20:50 +0000</pubDate>
				<category><![CDATA[Two pointers]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[sort]]></category>
		<category><![CDATA[two pointers]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6517</guid>

					<description><![CDATA[<p>Given two integer arrays&#160;arr1&#160;and&#160;arr2, and the integer&#160;d,&#160;return the distance value between the two&#160;arrays. The distance value is defined as the number of elements&#160;arr1[i]&#160;such that there&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-1385-find-the-distance-value-between-two-arrays/">花花酱 LeetCode 1385. Find the Distance Value Between Two Arrays</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 integer arrays&nbsp;<code>arr1</code>&nbsp;and&nbsp;<code>arr2</code>, and the integer&nbsp;<code>d</code>,&nbsp;<em>return the distance value between the two&nbsp;arrays</em>.</p>



<p>The distance value is defined as the number of elements&nbsp;<code>arr1[i]</code>&nbsp;such that there is not any element&nbsp;<code>arr2[j]</code>&nbsp;where&nbsp;<code>|arr1[i]-arr2[j]| &lt;= d</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
For arr1[0]=4 we have: 
|4-10|=6 &gt; d=2 
|4-9|=5 &gt; d=2 
|4-1|=3 &gt; d=2 
|4-8|=4 &gt; d=2 
For arr1[1]=5 we have: 
|5-10|=5 &gt; d=2 
|5-9|=4 &gt; d=2 
|5-1|=4 &gt; d=2 
|5-8|=3 &gt; d=2
For arr1[2]=8 we have:
<strong>|8-10|=2 &lt;= d=2</strong>
<strong>|8-9|=1 &lt;= d=2</strong>
|8-1|=7 &gt; d=2
<strong>|8-8|=0 &lt;= d=2</strong>
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3
<strong>Output:</strong> 2
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6
<strong>Output:</strong> 1
</pre>



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



<ul><li><code>1 &lt;= arr1.length, arr2.length &lt;= 500</code></li><li><code>-10^3 &lt;= arr1[i], arr2[j] &lt;= 10^3</code></li><li><code>0 &lt;= d &lt;= 100</code></li></ul>



<h2><strong>Solution 1:  All pairs</strong></h2>



<p>Time complexity: O(m*n)<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 findTheDistanceValue(vector&lt;int&gt;&amp; arr1, vector&lt;int&gt;&amp; arr2, int d) {    
    int ans = 0;
    for (int x : arr1) {
      bool flag = true;
      for (int y : arr2)
        if (abs(x - y) &lt;= d) {
          flag = false;
          break;
        }
      ans += flag;
    }
    return ans;
  }
};</pre>
</div></div>



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

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -&gt; int:
    return sum(all(abs(x - y) &gt; d for y in arr2) for x in arr1)</pre>
</div></div>



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



<p>Sort arr1 in ascending order and sort arr2 in descending order.<br>Time complexity: O(mlogm + nlogn + m + n)<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 findTheDistanceValue(vector&lt;int&gt;&amp; arr1, vector&lt;int&gt;&amp; arr2, int d) {    
    sort(begin(arr1), end(arr1));
    sort(rbegin(arr2), rend(arr2));
    int ans = 0;
    for (int a : arr1) {
      while (arr2.size() &amp;&amp; arr2.back() &lt; a - d)
        arr2.pop_back();
      ans += arr2.empty() || arr2.back() &gt; a + d;
    }
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 3: Binary Search</strong></h2>



<p>Sort arr2 in ascending order. and do two binary searches for each element to determine the range of [a-d, a+d], if that range is empty we increase the counter</p>



<p>Time complexity: O(mlogm + nlogm)<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 findTheDistanceValue(vector&lt;int&gt;&amp; arr1, vector&lt;int&gt;&amp; arr2, int d) {    
    sort(begin(arr2), end(arr2));
    int ans = 0;
    for (int a : arr1) {
      auto it1 = lower_bound(begin(arr2), end(arr2), a - d);
      auto it2 = upper_bound(begin(arr2), end(arr2), a + d);
      if (it1 == it2) ++ans;
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-1385-find-the-distance-value-between-two-arrays/">花花酱 LeetCode 1385. Find the Distance Value Between Two Arrays</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-1385-find-the-distance-value-between-two-arrays/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1237. Find Positive Integer Solution for a Given Equation</title>
		<link>https://zxi.mytechroad.com/blog/brute-force/leetcode-1237-find-positive-integer-solution-for-a-given-equation/</link>
					<comments>https://zxi.mytechroad.com/blog/brute-force/leetcode-1237-find-positive-integer-solution-for-a-given-equation/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 28 Oct 2019 03:16:56 +0000</pubDate>
				<category><![CDATA[Brute Force]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[interactive]]></category>
		<category><![CDATA[O(mn)]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5785</guid>

					<description><![CDATA[<p>Given a&#160;function&#160;&#160;f(x, y)&#160;and a value&#160;z, return all positive integer&#160;pairs&#160;x&#160;and&#160;y&#160;where&#160;f(x,y) == z. The function is constantly increasing, i.e.: f(x, y) &#60; f(x + 1, y) f(x,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/brute-force/leetcode-1237-find-positive-integer-solution-for-a-given-equation/">花花酱 LeetCode 1237. Find Positive Integer Solution for a Given Equation</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;function&nbsp;&nbsp;<code>f(x, y)</code>&nbsp;and a value&nbsp;<code>z</code>, return all positive integer&nbsp;pairs&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>&nbsp;where&nbsp;<code>f(x,y) == z</code>.</p>



<p>The function is constantly increasing, i.e.:</p>



<ul><li><code>f(x, y) &lt; f(x + 1, y)</code></li><li><code>f(x, y) &lt; f(x, y + 1)</code></li></ul>



<p>The function interface is defined like this:&nbsp;</p>



<pre class="wp-block-preformatted;crayon:false">interface CustomFunction {
public:
&nbsp; // Returns positive integer f(x, y) for any given positive integer x and y.
&nbsp; int f(int x, int y);
};
</pre>



<p>For custom testing purposes you&#8217;re given an integer&nbsp;<code>function_id</code>&nbsp;and a target&nbsp;<code>z</code>&nbsp;as input, where&nbsp;<code>function_id</code>&nbsp;represent one function from an secret internal list, on the examples you&#8217;ll know only two functions from the list. &nbsp;</p>



<p>You may return the solutions in any order.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> function_id = 1, z = 5
<strong>Output:</strong> [[1,4],[2,3],[3,2],[4,1]]
<strong>Explanation:</strong>&nbsp;function_id = 1 means that f(x, y) = x + y</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> function_id = 2, z = 5
<strong>Output:</strong> [[1,5],[5,1]]
<strong>Explanation:</strong>&nbsp;function_id = 2 means that f(x, y) = x * y
</pre>



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



<ul><li><code>1 &lt;= function_id &lt;= 9</code></li><li><code>1 &lt;= z &lt;= 100</code></li><li>It&#8217;s guaranteed that the solutions of&nbsp;<code>f(x, y) == z</code>&nbsp;will be on the range&nbsp;<code>1 &lt;= x, y &lt;= 1000</code></li><li>It&#8217;s also guaranteed that&nbsp;<code>f(x, y)</code>&nbsp;will fit in 32 bit signed integer if&nbsp;<code>1 &lt;= x, y &lt;= 1000</code></li></ul>



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



<p>Time complexity: O(1000*1000)<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; findSolution(CustomFunction&amp; customfunction, int z) {
    vector&lt;vector&lt;int&gt;&gt; ans;    
    for (int x = 1; x &lt;= 1000; ++x) {
      if (customfunction.f(x, 1) &gt; z) break;
      for (int y = 1; y &lt;= 1000; ++y) {
        int t = customfunction.f(x, y);
        if (t &gt; z) break;
        if (t == z) ans.push_back({x, y});        
      }
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/brute-force/leetcode-1237-find-positive-integer-solution-for-a-given-equation/">花花酱 LeetCode 1237. Find Positive Integer Solution for a Given Equation</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/brute-force/leetcode-1237-find-positive-integer-solution-for-a-given-equation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 681. Next Closest Time</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-681-next-closest-time/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-681-next-closest-time/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 25 Sep 2017 04:01:14 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[brute force]]></category>
		<category><![CDATA[clock]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[time]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=403</guid>

					<description><![CDATA[<p>Problem: https://leetcode.com/problems/next-closest-time/description/ no limit on how many times a digit can be reused. You may assume the given input string is always valid. For example,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-681-next-closest-time/">花花酱 LeetCode 681. Next Closest Time</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/IAet94C1FCc?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem:</strong></h1>
<p><a href="https://leetcode.com/problems/next-closest-time/description/">https://leetcode.com/problems/next-closest-time/description/</a><br />
no limit on how many times a digit can be reused.</p>
<p>You may assume the given input string is always valid. For example, &#8220;01:34&#8221;, &#8220;12:09&#8221; are all valid. &#8220;1:34&#8221;, &#8220;12:9&#8221; are all invalid.</p>
<h2><b>Example 1:</b></h2>
<pre class="crayon:false">Input: "19:34"
Output: "19:39"
Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, 
which occurs 5 minutes later.  
It is not 19:33, because this occurs 23 hours and 59 minutes later.
</pre>
<h2><b>Example 2:</b></h2>
<pre class="crayon:false">Input: "23:59"
Output: "22:22"
Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22. 
It may be assumed that the returned time is next day's time since it is smaller 
than the input time numerically.</pre>
<p>Ads<br />
<ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"></ins></p>
<h1><strong>Solution1: Brute force</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 3 ms
class Solution {
public:
    string nextClosestTime(string time) {
        set&lt;char&gt; s(time.begin(), time.end());
        int hour = stoi(time.substr(0, 2));
        int min = stoi(time.substr(3, 2));
        while (true) {
            if (++min == 60) {
                min = 0;
                (++hour) %= 24;                
            }
            char buffer[5];
            sprintf(buffer, "%02d:%02d", hour, min);
            set&lt;char&gt; s2(buffer, buffer + sizeof(buffer));
            if (includes(s.begin(), s.end(), s2.begin(), s2.end()))
                return string(buffer);
        }
        return time;
    }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 85 ms
class Solution {
    public String nextClosestTime(String time) {
        int hour = Integer.parseInt(time.substring(0, 2));
        int min = Integer.parseInt(time.substring(3, 5));
        while (true) {            
            if (++min == 60) {
                min = 0;
                ++hour;
                hour %= 24;
            }
            String curr = String.format("%02d:%02d", hour, min);
            Boolean valid = true;
            for (int i = 0; i &lt; curr.length(); ++i)
                if (time.indexOf(curr.charAt(i)) &lt; 0) {
                    valid = false;
                    break;
                }
            if (valid) return curr;
        }
    }
}</pre><p></div><h2 class="tabtitle">Python</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 65 ms
"""
class Solution:
    def nextClosestTime(self, time):        
        s = set(time)
        hour = int(time[0:2])
        minute = int(time[3:5])
        while True:
            minute += 1
            if minute == 60:
                minute = 0
                hour = 0 if hour == 23 else hour + 1
            
            time = "%02d:%02d" % (hour, minute)
            if set(time) &lt;= s:
                return time
        return time</pre><p></div></div></p>
<h1><strong>Solution 2: DFS</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua 
// Running time: 3 ms
class Solution {
public:
    string nextClosestTime(string time) {
        vector&lt;int&gt; d = {
            time[0] - '0', time[1] - '0', time[3] - '0', time[4] - '0' };
        
        int h = d[0] * 10 + d[1];
        int m = d[2] * 10 + d[3];
        vector&lt;int&gt; curr(4, 0);
        int now = toTime(h, m);
        int best = now;
        
        dfs(0, d, curr, now, best);
        char buff[5];
        sprintf(buff, "%02d:%02d", best / 60, best % 60);
        return string(buff);
    }
private:
    void dfs(int dep, const vector&lt;int&gt;&amp; digits, vector&lt;int&gt;&amp; curr, int time, int&amp; best) {
        if (dep == 4) {
            int curr_h = curr[0] * 10 + curr[1];
            int curr_m = curr[2] * 10 + curr[3];
            if (curr_h &gt; 23 || curr_m &gt; 59) return;
            int curr_time = toTime(curr_h, curr_m);
            if (timeDiff(time, curr_time) &lt; timeDiff(time, best))
                best = curr_time;
            return;
        }            
        
        for (int digit : digits) {
            curr[dep] = digit;
            dfs(dep + 1, digits, curr, time, best);
        }        
    }
    
    int toTime(int h, int m) {
        return h * 60 + m;
    }
    
    int timeDiff(int t1, int t2) {
        if (t1 == t2) return INT_MAX;
        return ((t2 - t1) + 24 * 60) % (24 * 60);
    }
};</pre><p></div></div></p>
<h1><strong>Solution 3: Brute force + Time library</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 292 ms
"""
from datetime import *
class Solution:
    def nextClosestTime(self, time):        
        s = set(time)        
        while True:
            time = (datetime.strptime(time, '%H:%M') + 
                    timedelta(minutes=1)).strftime('%H:%M')
            if set(time) &lt;= s: 
                return time
        return time</pre><p></div></div></p>
<h1>Related Problems:</h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/bit/leetcode-401-binary-watch/">花花酱 LeetCode 401. Binary Watch</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-681-next-closest-time/">花花酱 LeetCode 681. Next Closest Time</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/simulation/leetcode-681-next-closest-time/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
