<?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>quad tree Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/quad-tree/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/quad-tree/</link>
	<description></description>
	<lastBuildDate>Sun, 01 Dec 2019 21:11:52 +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>quad tree Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/quad-tree/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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 427. Construct Quad Tree</title>
		<link>https://zxi.mytechroad.com/blog/geometry/leetcode-427-construct-quad-tree/</link>
					<comments>https://zxi.mytechroad.com/blog/geometry/leetcode-427-construct-quad-tree/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 18 Jul 2018 05:59:36 +0000</pubDate>
				<category><![CDATA[Geometry]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[quad tree]]></category>
		<category><![CDATA[recursion]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3211</guid>

					<description><![CDATA[<p>Problem We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or false. The&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-427-construct-quad-tree/">花花酱 LeetCode 427. Construct Quad Tree</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>We want to use quad trees to store an <code>N x N</code> boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodes <strong>until the values in the region it represents are all the same</strong>.</p>
<p>Each node has another two boolean attributes : <code>isLeaf</code> and <code>val</code>. <code>isLeaf</code> is true if and only if the node is a leaf node. The <code>val</code> attribute for a leaf node contains the value of the region it represents.</p>
<p>Your task is to use a quad tree to represent a given grid. The following example may help you understand the problem better:</p>
<p>Given the <code>8 x 8</code> grid below, we want to construct the corresponding quad tree:</p>
<p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/01/962_grid.png" alt="" /></p>
<p>It can be divided according to the definition above:</p>
<p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/01/962_grid_divided.png" alt="" /></p>
<p>&nbsp;</p>
<p>The corresponding quad tree should be as following, where each node is represented as a <code>(isLeaf, val)</code>pair.</p>
<p>For the non-leaf nodes, <code>val</code> can be arbitrary, so it is represented as <code>*</code>.</p>
<p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/01/962_quad_tree.png" alt="" /></p>
<p><strong>Note:</strong></p>
<ol>
<li><code>N</code> is less than <code>1000</code> and guaranteened to be a power of 2.</li>
<li>If you want to know more about the quad tree, you can refer to its <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</li>
</ol>
<h1><strong>Solution: Recursion</strong></h1>
<p>Time complexity: O(n^2*logn)</p>
<p>Space complexity: O(n^2)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 44 ms
class Solution {
public:
  Node* construct(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {    
    return construct(grid, 0, 0, grid.size());
  }
private:
  Node* construct(const vector&lt;vector&lt;int&gt;&gt;&amp; grid, int x, int y, int n) {
    if (n == 0) return nullptr;    
    bool all_zeros = true;
    bool all_ones = true;
    for (int i = y; i &lt; y + n; ++i)
      for (int j = x; j &lt; x + n; ++j)
        if (grid[i][j] == 0)
          all_ones = false;
        else
          all_zeros = false;
    if (all_zeros || all_ones)
      return new Node(all_ones, true, nullptr, nullptr, nullptr, nullptr);
    
    return new Node(true, false,
                    construct(grid, x,       y,       n/2),  // topLeft
                    construct(grid, x + n/2, y,       n/2),  // topRight
                    construct(grid, x,       y + n/2, n/2),  // bottomLeft
                    construct(grid, x + n/2, y + n/2, n/2)); // bottomRight
  }
};</pre><p>V2</p>
<p>Time complexity: O(n^2)</p>
<p>Space complexity: O(n^2)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 56 ms
class Solution {
public:
  Node* construct(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {    
    return construct(grid, 0, 0, grid.size());
  }
private:
  Node* construct(const vector&lt;vector&lt;int&gt;&gt;&amp; grid, int x, int y, int n) {
    if (n == 0) return nullptr;    
    if (n == 1) return new Node(grid[y][x], true, nullptr, nullptr, nullptr, nullptr);
    
    auto tl = construct(grid, x,       y,       n/2);   // topLeft
    auto tr = construct(grid, x + n/2, y,       n/2);   // topRight
    auto bl = construct(grid, x,       y + n/2, n/2);   // bottomLeft
    auto br = construct(grid, x + n/2, y + n/2, n/2);   // bottomRight
    
    if (tl-&gt;isLeaf &amp;&amp; tr-&gt;isLeaf &amp;&amp; bl-&gt;isLeaf &amp;&amp; br-&gt;isLeaf 
    &amp;&amp; tl-&gt;val == tr-&gt;val &amp;&amp; tl-&gt;val == bl-&gt;val &amp;&amp; tl-&gt;val == br-&gt;val) {
      auto root = new Node(tl-&gt;val, true, nullptr, nullptr, nullptr, nullptr);
      delete tl;
      delete tr;
      delete bl;
      delete br;
      return root;
    }
    
    return new Node(true, false, tl, tr, bl, br);                    
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/geometry/leetcode-427-construct-quad-tree/">花花酱 LeetCode 427. Construct Quad Tree</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-427-construct-quad-tree/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
