<?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>king Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/king/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/king/</link>
	<description></description>
	<lastBuildDate>Tue, 15 Oct 2019 07:30:40 +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>king Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/king/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1222. Queens That Can Attack the King</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-1222-queens-that-can-attack-the-king/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-1222-queens-that-can-attack-the-king/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 14 Oct 2019 14:48:56 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[chess]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[king]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[queen]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5757</guid>

					<description><![CDATA[<p>On an&#160;8&#215;8&#160;chessboard, there can be multiple Black Queens and one White King. Given an array of integer coordinates&#160;queens&#160;that represents the positions of the Black Queens,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1222-queens-that-can-attack-the-king/">花花酱 LeetCode 1222. Queens That Can Attack the King</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 an&nbsp;<strong>8&#215;8</strong>&nbsp;chessboard, there can be multiple Black Queens and one White King.</p>



<p>Given an array of integer coordinates&nbsp;<code>queens</code>&nbsp;that represents the positions of the Black Queens, and a pair of coordinates&nbsp;<code>king</code>&nbsp;that represent the position of the White King, return the coordinates of all the queens (in any order) that can attack the King.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/10/01/untitled-diagram.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> queens = [[0,1],[1,0],[4,0],[0,4],[3,3],[2,4]], king = [0,0]
<strong>Output:</strong> [[0,1],[1,0],[3,3]]
<strong>Explanation:</strong>&nbsp; 
The queen at [0,1] can attack the king cause they're in the same row. 
The queen at [1,0] can attack the king cause they're in the same column. 
The queen at [3,3] can attack the king cause they're in the same diagnal. 
The queen at [0,4] can't attack the king cause it's blocked by the queen at [0,1]. 
The queen at [4,0] can't attack the king cause it's blocked by the queen at [1,0]. 
The queen at [2,4] can't attack the king cause it's not in the same row/column/diagnal as the king.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/10/01/untitled-diagram-1.jpg" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> queens = [[0,0],[1,1],[2,2],[3,4],[3,5],[4,4],[4,5]], king = [3,3]
<strong>Output:</strong> [[2,2],[3,4],[4,4]]
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/10/01/untitled-diagram-2.jpg" alt=""/></figure>



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



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



<ul><li><code>1 &lt;= queens.length&nbsp;&lt;= 63</code></li><li><code>queens[0].length == 2</code></li><li><code>0 &lt;= queens[i][j] &lt;&nbsp;8</code></li><li><code>king.length == 2</code></li><li><code>0 &lt;= king[0], king[1] &lt; 8</code></li><li>At most one piece is allowed in a cell.</li></ul>



<h2><strong>Solution2: Simulation</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:
  vector&lt;vector&lt;int&gt;&gt; queensAttacktheKing(vector&lt;vector&lt;int&gt;&gt;&amp; queens, vector&lt;int&gt;&amp; king) {    
    vector&lt;vector&lt;int&gt;&gt; b(8, vector&lt;int&gt;(8));
    for (const auto&amp; q : queens)
      b[q[0]][q[1]] = 1;
    vector&lt;vector&lt;int&gt;&gt; ans;
    for (const auto&amp; q : queens) {
      for (int dx = -1; dx &lt;= 1; ++dx)
        for (int dy = -1; dy &lt;= 1; ++dy) {
          if (dx == 0 &amp;&amp; dy == 0) continue;
          int x = q[1] + dx;
          int y = q[0] + dy;
          while (x &gt;= 0 &amp;&amp; y &gt;= 0 &amp;&amp; x &lt; 8 &amp;&amp; y &lt; 8 &amp;&amp; !b[y][x]) {
            if (x == king[1] &amp;&amp; y == king[0])
              ans.push_back(q);            
            x += dx;
            y += dy;
          }
        }
    }
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: HashTable + Binary Search</strong></h2>



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



<p>Support arbitrarily large boards, e.g. 1e9 x 1e9 with 1e6  # of queens.</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; queensAttacktheKing(vector&lt;vector&lt;int&gt;&gt;&amp; queens, vector&lt;int&gt;&amp; king) {    
    unordered_map&lt;int, map&lt;int, vector&lt;int&gt;&gt;&gt; rows, cols, diag1, diag2;    
    for (const auto&amp; q : queens) {
      const int x = q[1];
      const int y = q[0];
      rows[y][x] = q;
      cols[x][y] = q;
      diag1[x + y][x] = q;
      diag2[x - y][x] = q;
    }
    
    const int x = king[1];
    const int y = king[0];
    vector&lt;vector&lt;int&gt;&gt; ans;
    find(rows, y, x, ans);
    find(cols, x, y, ans);
    find(diag1, x + y, x, ans);
    find(diag2, x - y, x, ans);
    return ans;
  }
private:
  void find(const unordered_map&lt;int, map&lt;int, vector&lt;int&gt;&gt;&gt;&amp; m, int idx, int key, vector&lt;vector&lt;int&gt;&gt;&amp; ans) {    
    if (!m.count(idx)) return;
    const auto&amp; d = m.at(idx);
    auto it = d.upper_bound(key);
    if (it != end(d))
      ans.push_back(it-&gt;second);
    if (it != begin(d))
      ans.push_back(prev(it)-&gt;second);    
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1222-queens-that-can-attack-the-king/">花花酱 LeetCode 1222. Queens That Can Attack the King</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-1222-queens-that-can-attack-the-king/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
