<?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>interactive Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/interactive/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/interactive/</link>
	<description></description>
	<lastBuildDate>Sat, 14 Nov 2020 06:49:29 +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>interactive Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/interactive/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 1095. Find in Mountain Array &#8211; EP369</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/1095-find-in-mountain-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/1095-find-in-mountain-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 14 Nov 2020 05:57:59 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[interactive]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7638</guid>

					<description><![CDATA[<p>(This problem is an&#160;interactive problem.) You may recall that an array&#160;A&#160;is a&#160;mountain array&#160;if and only if: A.length &#62;= 3 There exists some&#160;i&#160;with&#160;0 &#60; i&#160;&#60; A.length&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/1095-find-in-mountain-array/">花花酱 1095. Find in Mountain Array &#8211; EP369</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1095. Find in Mountain Array - 刷题找工作 EP369" width="500" height="281" src="https://www.youtube.com/embed/G5IDEoX9bT0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p><em>(This problem is an&nbsp;<strong>interactive problem</strong>.)</em></p>



<p>You may recall that an array&nbsp;<code>A</code>&nbsp;is a&nbsp;<em>mountain array</em>&nbsp;if and only if:</p>



<ul><li><code>A.length &gt;= 3</code></li><li>There exists some&nbsp;<code>i</code>&nbsp;with&nbsp;<code>0 &lt; i&nbsp;&lt; A.length - 1</code>&nbsp;such that:<ul><li><code>A[0] &lt; A[1] &lt; ... A[i-1] &lt; A[i]</code></li><li><code>A[i] &gt; A[i+1] &gt; ... &gt; A[A.length - 1]</code></li></ul></li></ul>



<p>Given a mountain&nbsp;array&nbsp;<code>mountainArr</code>, return the&nbsp;<strong>minimum</strong>&nbsp;<code>index</code>&nbsp;such that&nbsp;<code>mountainArr.get(index) == target</code>.&nbsp; If such an&nbsp;<code>index</code>&nbsp;doesn&#8217;t exist, return&nbsp;<code>-1</code>.</p>



<p><strong>You can&#8217;t access the mountain array directly.</strong>&nbsp; You may only access the array using a&nbsp;<code>MountainArray</code>&nbsp;interface:</p>



<ul><li><code>MountainArray.get(k)</code>&nbsp;returns the element of the array at index&nbsp;<code>k</code>&nbsp;(0-indexed).</li><li><code>MountainArray.length()</code>&nbsp;returns the length of the array.</li></ul>



<p>Submissions making more than&nbsp;<code>100</code>&nbsp;calls to&nbsp;<code>MountainArray.get</code>&nbsp;will be judged&nbsp;<em>Wrong Answer</em>.&nbsp; Also, any solutions that attempt to circumvent the judge&nbsp;will result in disqualification.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> array = [1,2,3,4,5,3,1], target = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> array = [0,1,2,4,2,1], target = 3
<strong>Output:</strong> -1
<strong>Explanation:</strong> 3 does not exist in <code>the array,</code> so we return -1.
</pre>



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



<ul><li><code>3 &lt;= mountain_arr.length() &lt;= 10000</code></li><li><code>0 &lt;= target &lt;= 10^9</code></li><li><code>0 &lt;= mountain_arr.get(index) &lt;=&nbsp;10^9</code></li></ul>



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



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1095-ep369.png" alt="" class="wp-image-7641" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1095-ep369.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1095-ep369-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/11/1095-ep369-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<ol><li>Find the peak index of the mountain array using binary search.</li><li>Perform two binary searches in two sorted subarrays (ascending one and descending one)</li></ol>



<p>Time complexity: O(logn)<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:
  int findInMountainArray(int target, MountainArray &amp;mountainArr) {
    const int n = mountainArr.length();
    
    auto binary_search = [&amp;](int l, int r, function&lt;bool(int)&gt; cond) {
      while (l &lt; r) {
        int m = l + (r - l) / 2;
        if (cond(m)) r = m;
        else l = m + 1;
      }  
      return l;
    };
    
    int p = binary_search(0, n - 1, [&amp;](int i) -&gt; bool {
      return mountainArr.get(i) &gt; mountainArr.get(i + 1);
    });    
    
    // if (target &gt; mountainArr.get(p)) return -1;
    // if (target == mountainArr.get(p)) return p;
    
    int l = binary_search(0, p, [&amp;](int i) -&gt; bool {
      return mountainArr.get(i) &gt;= target;
    });
    
    if (mountainArr.get(l) == target) return l;
    
    int r = binary_search(p, n - 1, [&amp;](int i) -&gt; bool {
      return mountainArr.get(i) &lt;= target;
    });        
    
    if (mountainArr.get(r) == target) return r;
    
    return -1;
  }
};</pre>

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

<pre class="crayon-plain-tag">class Solution:
  def findInMountainArray(self, target: int, arr: 'MountainArray') -&gt; int:
    def binary_search(l, r, cond):
      while l &lt; r:
        m = l + (r - l) // 2
        if cond(m): r = m
        else: l = m + 1
      return l
    
    n = arr.length()    
    p = binary_search(0, n - 1, lambda i: arr.get(i) &gt; arr.get(i + 1))
    l = binary_search(0, p, lambda i: arr.get(i) &gt;= target)
    if arr.get(l) == target: return l
    r = binary_search(p, n - 1, lambda i: arr.get(i) &lt;= target)
    if arr.get(r) == target: return r
    return -1</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/1095-find-in-mountain-array/">花花酱 1095. Find in Mountain Array &#8211; EP369</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/1095-find-in-mountain-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1274. Number of Ships in a Rectangle</title>
		<link>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-1274-number-of-ships-in-a-rectangle/</link>
					<comments>https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-1274-number-of-ships-in-a-rectangle/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 01 Dec 2019 21:06:00 +0000</pubDate>
				<category><![CDATA[Divide and conquer]]></category>
		<category><![CDATA[divide and conquer]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[interactive]]></category>
		<category><![CDATA[quad tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5900</guid>

					<description><![CDATA[<p>(This problem is an&#160;interactive problem.) On the sea represented by a cartesian plane, each ship is located at an integer point, and each integer point&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-1274-number-of-ships-in-a-rectangle/">花花酱 LeetCode 1274. Number of Ships in a Rectangle</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><em>(This problem is an&nbsp;<strong>interactive problem</strong>.)</em></p>



<p>On the sea represented by a cartesian plane, each ship is located at an integer point, and each integer point may contain at most 1 ship.</p>



<p>You have a function&nbsp;<code>Sea.hasShips(topRight, bottomLeft)</code>&nbsp;which takes two points&nbsp;as arguments and returns&nbsp;<code>true</code>&nbsp;if and only if there is at least one ship in the rectangle represented by the two points, including on the boundary.</p>



<p>Given two points, which are the top right and bottom left corners of a rectangle, return the number of ships present in that rectangle.&nbsp;&nbsp;It is guaranteed that there are&nbsp;<strong>at most 10 ships</strong>&nbsp;in that rectangle.</p>



<p>Submissions making&nbsp;<strong>more than 400 calls</strong>&nbsp;to&nbsp;<code>hasShips</code>&nbsp;will be judged&nbsp;<em>Wrong Answer</em>.&nbsp; Also, any solutions that attempt to circumvent the judge&nbsp;will result in disqualification.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/07/26/1445_example_1.PNG" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> 
ships = [[1,1],[2,2],[3,3],[5,5]], topRight = [4,4], bottomLeft = [0,0]
<strong>Output:</strong> 3
<strong>Explanation:</strong> From [0,0] to [4,4] we can count 3 ships within the range.
</pre>



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



<ul><li>On the input&nbsp;<code>ships</code>&nbsp;is only given to initialize the map internally.&nbsp;You must solve this problem &#8220;blindfolded&#8221;. In other words, you must find the answer using the given&nbsp;<code>hasShips</code>&nbsp;API, without knowing the&nbsp;<code>ships</code>&nbsp;position.</li><li><code>0 &lt;=&nbsp;bottomLeft[0]&nbsp;&lt;= topRight[0]&nbsp;&lt;= 1000</code></li><li><code>0 &lt;=&nbsp;bottomLeft[1]&nbsp;&lt;= topRight[1]&nbsp;&lt;= 1000</code></li></ul>



<h2><strong>Solution: Divide and Conquer</strong></h2>



<p>If the current rectangle contains ships, subdivide it into 4 smaller ones until <br>1) no ships contained<br>2) the current rectangle is a single point (e.g. topRight == bottomRight)</p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  // Modify the interface to pass sea as a reference.
  int countShips(Sea&amp; sea, vector&lt;int&gt; topRight, vector&lt;int&gt; bottomLeft) {
    int x1 = bottomLeft[0], y1 = bottomLeft[1];
    int x2 = topRight[0], y2 = topRight[1];    
    if (x1 &gt; x2 || y1 &gt; y2 || !sea.hasShips(topRight, bottomLeft))
      return 0;
    if (x1 == x2 &amp;&amp; y1 == y2)
      return 1;
    int xm = x1 + (x2 - x1) / 2;
    int ym = y1 + (y2 - y1) / 2;
    return countShips(sea, {xm, ym}, {x1, y1}) + countShips(sea, {xm, y2}, {x1, ym + 1})
         + countShips(sea, {x2, ym}, {xm + 1, y1}) + countShips(sea, {x2, y2}, {xm + 1, ym + 1});
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-1274-number-of-ships-in-a-rectangle/">花花酱 LeetCode 1274. Number of Ships in a Rectangle</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/divide-and-conquer/leetcode-1274-number-of-ships-in-a-rectangle/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 278. First Bad Version</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-278-first-bad-version/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-278-first-bad-version/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 15 Mar 2018 16:15:29 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[interactive]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2095</guid>

					<description><![CDATA[<p>题目大意：给你一个API查询版本是否坏了，让你找出第一个坏掉的版本。 Problem: https://leetcode.com/problems/first-bad-version/description/ You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-278-first-bad-version/">花花酱 LeetCode 278. First Bad Version</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>题目大意：给你一个API查询版本是否坏了，让你找出第一个坏掉的版本。</p>
<p><strong>Problem:</strong></p>
<p><a href="https://leetcode.com/problems/first-bad-version/description/">https://leetcode.com/problems/first-bad-version/description/</a></p>
<p>You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.</p>
<p>Suppose you have <code>n</code> versions <code>[1, 2, ..., n]</code> and you want to find out the first bad one, which causes all the following ones to be bad.</p>
<p>You are given an API <code>bool isBadVersion(version)</code> which will return whether <code>version</code> is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.</p>
<p><strong>Solution 1: Brute Force</strong></p>
<p>Time Complexity: O(n) TLE</p>
<p>Space Complexity: O(1)</p>
<p><strong>Solution 2: Binary Search</strong></p>
<p>Time Complexity: O(logn)</p>
<p>Space Complexity: O(1)</p><pre class="crayon-plain-tag">// Forward declaration of isBadVersion API.
bool isBadVersion(int version);

// Author: Huahua
// Running time: 2 ms
class Solution {
public:
  int firstBadVersion(int n) {
    int l = 1;
    int r = n;
    while (l &lt; r) {
      int m = l + (r - l) / 2;
      if (isBadVersion(m))
        r = m;
      else
        l = m + 1;
    }
    return l;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-278-first-bad-version/">花花酱 LeetCode 278. First Bad Version</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-278-first-bad-version/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
