<?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>nqueen Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/nqueen/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/nqueen/</link>
	<description></description>
	<lastBuildDate>Sun, 24 Feb 2019 18:26:47 +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>nqueen Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/nqueen/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1001. Grid Illumination</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1001-grid-illumination/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1001-grid-illumination/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 24 Feb 2019 18:18:24 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[board]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[nqueen]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4896</guid>

					<description><![CDATA[<p>On a&#160;N x N&#160;grid of cells, each cell&#160;(x, y)&#160;with&#160;0 &#60;= x &#60; N&#160;and&#160;0 &#60;= y &#60; N&#160;has a lamp. Initially, some number of lamps are&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1001-grid-illumination/">花花酱 LeetCode 1001. Grid Illumination</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>On a&nbsp;<code>N x N</code>&nbsp;grid of cells, each cell&nbsp;<code>(x, y)</code>&nbsp;with&nbsp;<code>0 &lt;= x &lt; N</code>&nbsp;and&nbsp;<code>0 &lt;= y &lt; N</code>&nbsp;has a lamp.</p>



<p>Initially, some number of lamps are on.&nbsp;&nbsp;<code>lamps[i]</code>&nbsp;tells us the location of the&nbsp;<code>i</code>-th lamp that is on.&nbsp; Each lamp that is on illuminates every square on its x-axis, y-axis, and both diagonals (similar to a Queen in chess).</p>



<p>For the i-th query&nbsp;<code>queries[i] = (x, y)</code>, the answer to the query is 1 if the cell (x, y) is illuminated, else 0.</p>



<p>After each query&nbsp;<code>(x, y)</code>&nbsp;[in the order given by&nbsp;<code>queries</code>], we turn off any&nbsp;lamps that are at cell&nbsp;<code>(x, y)</code>&nbsp;or are adjacent 8-directionally (ie., share a corner or edge with cell&nbsp;<code>(x, y)</code>.)</p>



<p>Return an array of answers.&nbsp; Each&nbsp;value&nbsp;<code>answer[i]</code>&nbsp;should be equal to the answer of the&nbsp;<code>i</code>-th query&nbsp;<code>queries[i]</code>.</p>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>N = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,0]]
<strong>Output: </strong>[1,0]
<strong>Explanation: </strong>
Before performing the first query we have both lamps [0,0] and [4,4] on.
The grid representing which cells are lit looks like this, where [0,0] is the top left corner, and [4,4] is the bottom right corner:
1 1 1 1 1
1 1 0 0 1
1 0 1 0 1
1 0 0 1 1
1 1 1 1 1
Then the query at [1, 1] returns 1 because the cell is lit.  After this query, the lamp at [0, 0] turns off, and the grid now looks like this:
1 0 0 0 1
0 1 0 0 1
0 0 1 0 1
0 0 0 1 1
1 1 1 1 1
Before performing the second query we have only the lamp [4,4] on.  Now the query at [1,0] returns 0, because the cell is no longer lit.
</pre>



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



<ol><li><code>1 &lt;= N &lt;= 10^9</code></li><li><code>0 &lt;= lamps.length &lt;= 20000</code></li><li><code>0 &lt;= queries.length &lt;= 20000</code></li><li><code>lamps[i].length == queries[i].length == 2</code></li></ol>



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



<p>use lx, ly, lp, lq to track the # of lamps that covers each row, col, diagonal, antidiagonal</p>



<p>Time complexity: O(|L| + |Q|)<br>Space complexity: O(|L|)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua, running time: 460 ms, 82.2 MB
class Solution {
public:
  vector&lt;int&gt; gridIllumination(int N, vector&lt;vector&lt;int&gt;&gt;&amp; lamps, vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    unordered_set&lt;long&gt; s;
    unordered_map&lt;int, int&gt; lx, ly, lp, lq;
    for (const auto&amp; lamp : lamps) {
      const int x = lamp[0];
      const int y = lamp[1];
      s.insert(static_cast&lt;long&gt;(x) &lt;&lt; 32 | y);
      ++lx[x];
      ++ly[y];
      ++lp[x + y];
      ++lq[x - y];
    }
    vector&lt;int&gt; ans;
    for (const auto&amp; query : queries) {
      const int x = query[0];
      const int y = query[1];      
      if (lx.count(x) || ly.count(y) || lp.count(x + y) || lq.count(x - y)) {
        ans.push_back(1);       
        for (int tx = x - 1; tx &lt;= x + 1; ++tx)
          for (int ty = y - 1; ty &lt;= y + 1; ++ty) {
            if (tx &lt; 0 || tx &gt;= N || ty &lt; 0 || ty &gt;= N) continue;
            const long key = static_cast&lt;long&gt;(tx) &lt;&lt; 32 | ty;
            if (!s.count(key)) continue;
            s.erase(key);
            if (--lx[tx] == 0) lx.erase(tx);
            if (--ly[ty] == 0) ly.erase(ty);
            if (--lp[tx + ty] == 0) lp.erase(tx + ty);
            if (--lq[tx - ty] == 0) lq.erase(tx - ty);
          }
      } else {
        ans.push_back(0);
      }
    }
    return ans;
  }
};</pre>

</div><h2 class="tabtitle">C++ v2</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, running time: 444 ms, 82.2 MB
class Solution {
public:
  vector&lt;int&gt; gridIllumination(int N, vector&lt;vector&lt;int&gt;&gt;&amp; lamps, vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    unordered_set&lt;long&gt; s;
    unordered_map&lt;int, int&gt; lx, ly, lp, lq;
    for (const auto&amp; lamp : lamps) {
      const int x = lamp[0];
      const int y = lamp[1];
      s.insert(static_cast&lt;long&gt;(x) &lt;&lt; 32 | y);
      ++lx[x];
      ++ly[y];
      ++lp[x + y];
      ++lq[x - y];
    }
    vector&lt;int&gt; ans;
    auto dec = [](unordered_map&lt;int, int&gt;&amp; m, int key) {
      auto it = m.find(key);
      if (it-&gt;second == 1)
        m.erase(it);
      else
        --it-&gt;second;
    };
    for (const auto&amp; query : queries) {
      const int x = query[0];
      const int y = query[1];      
      if (lx.count(x) || ly.count(y) || lp.count(x + y) || lq.count(x - y)) {
        ans.push_back(1);       
        for (int tx = x - 1; tx &lt;= x + 1; ++tx)
          for (int ty = y - 1; ty &lt;= y + 1; ++ty) {
            if (tx &lt; 0 || tx &gt;= N || ty &lt; 0 || ty &gt;= N) continue;
            const long key = static_cast&lt;long&gt;(tx) &lt;&lt; 32 | ty;
            auto it = s.find(key);
            if (it == s.end()) continue;
            s.erase(it);
            dec(lx, tx);
            dec(ly, ty);
            dec(lp, tx + ty);
            dec(lq, tx - ty);            
          }
      } else {
        ans.push_back(0);
      }
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1001-grid-illumination/">花花酱 LeetCode 1001. Grid Illumination</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-1001-grid-illumination/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
