<?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>binary mask Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/binary-mask/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/binary-mask/</link>
	<description></description>
	<lastBuildDate>Sun, 22 Mar 2020 20:54:42 +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>binary mask Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/binary-mask/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1386. Cinema Seat Allocation</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1386-cinema-seat-allocation/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1386-cinema-seat-allocation/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 22 Mar 2020 20:34:07 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[binary mask]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[hashtable]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6533</guid>

					<description><![CDATA[<p>A cinema&#160;has&#160;n&#160;rows of seats, numbered from 1 to&#160;n&#160;and there are ten&#160;seats in each row, labelled from 1&#160;to 10&#160;as shown in the figure above. Given the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1386-cinema-seat-allocation/">花花酱 LeetCode 1386. Cinema Seat Allocation</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-image"><img src="https://assets.leetcode.com/uploads/2020/02/14/cinema_seats_1.png" alt=""/></figure>



<p>A cinema&nbsp;has&nbsp;<code>n</code>&nbsp;rows of seats, numbered from 1 to&nbsp;<code>n</code>&nbsp;and there are ten&nbsp;seats in each row, labelled from 1&nbsp;to 10&nbsp;as shown in the figure above.</p>



<p>Given the array&nbsp;<code>reservedSeats</code>&nbsp;containing the numbers of seats already reserved, for example,&nbsp;<code>reservedSeats[i]=[3,8]</code>&nbsp;means the seat located in row&nbsp;<strong>3</strong>&nbsp;and labelled with&nbsp;<strong>8</strong>&nbsp;is already reserved.&nbsp;</p>



<p><em>Return the maximum number of four-person families you can allocate on the cinema&nbsp;seats.</em>&nbsp;A four-person family occupies fours seats&nbsp;<strong>in one row</strong>, that are&nbsp;<strong>next to each other</strong>. Seats across an aisle (such as [3,3]&nbsp;and [3,4]) are not considered to be next to each other, however, It is permissible for the four-person family to be separated by an aisle, but in that case,&nbsp;<strong>exactly two people</strong>&nbsp;have to sit on each side of the aisle.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/02/14/cinema_seats_3.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The figure above shows the optimal allocation for four families, where seats mark with blue are already reserved and contiguous seats mark with orange are for one family.&nbsp;
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 2, reservedSeats = [[2,1],[1,8],[2,6]]
<strong>Output:</strong> 2
</pre>



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



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



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



<ul><li><code>1 &lt;= n &lt;= 10^9</code></li><li><code>1 &lt;=&nbsp;reservedSeats.length &lt;= min(10*n, 10^4)</code></li><li><code>reservedSeats[i].length == 2</code></li><li><code>1&nbsp;&lt;=&nbsp;reservedSeats[i][0] &lt;= n</code></li><li><code>1 &lt;=&nbsp;reservedSeats[i][1] &lt;= 10</code></li><li>All&nbsp;<code>reservedSeats[i]</code>&nbsp;are distinct.</li></ul>



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



<p>if both seat[2~5] seat[6~9] are empty, seat two groups.<br>if any of seat[2~5] seat[4~7] seat[6~9] is empty seat one group.<br>if there is no one sit in a row, seat two groups.</p>



<p>Time complexity: O(|reservedSeats|)<br>Space complexity: O(|rows|)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maxNumberOfFamilies(int n, vector&lt;vector&lt;int&gt;&gt;&amp; reservedSeats) {
    unordered_map&lt;int, int&gt; rows;
    for (auto&amp; seat : reservedSeats)
      rows[seat[0]] |= 1 &lt;&lt; (seat[1] - 1);
    
    int ans = (n - rows.size()) * 2;
    
    for (const auto&amp; [idx, row] : rows) {
      int s2 = row &amp; 0b0000011110;
      int s4 = row &amp; 0b0001111000;
      int s6 = row &amp; 0b0111100000;
      if (s2 == 0 &amp;&amp; s6 == 0)
        ans += 2;
      else if (s2 == 0 || s4 == 0 || s6 == 0)
        ans += 1;
    }
    
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1386-cinema-seat-allocation/">花花酱 LeetCode 1386. Cinema Seat Allocation</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-1386-cinema-seat-allocation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1349. Maximum Students Taking Exam</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1349-maximum-students-taking-exam/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1349-maximum-students-taking-exam/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 09 Feb 2020 08:33:43 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[binary mask]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[state]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6275</guid>

					<description><![CDATA[<p>Given a&#160;m&#160;* n&#160;matrix&#160;seats&#160;&#160;that represent seats distributions&#160;in a classroom.&#160;If a seat&#160;is&#160;broken, it is denoted by&#160;'#'&#160;character otherwise it is denoted by a&#160;'.'&#160;character. Students can see the answers&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1349-maximum-students-taking-exam/">花花酱 LeetCode 1349. Maximum Students Taking Exam</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 1349. Maximum Students Taking Exam - 刷题找工作 EP307" width="500" height="281" src="https://www.youtube.com/embed/QJvCYx1eGxE?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given a&nbsp;<code>m&nbsp;* n</code>&nbsp;matrix&nbsp;<code>seats</code>&nbsp;&nbsp;that represent seats distributions&nbsp;in a classroom.&nbsp;If a seat&nbsp;is&nbsp;broken, it is denoted by&nbsp;<code>'#'</code>&nbsp;character otherwise it is denoted by a&nbsp;<code>'.'</code>&nbsp;character.</p>



<p>Students can see the answers of those sitting next to the left, right, upper left and upper right, but he cannot see the answers of the student sitting&nbsp;directly in front or behind him. Return the&nbsp;<strong>maximum&nbsp;</strong>number of students that can take the exam together&nbsp;without any cheating being possible..</p>



<p>Students must be placed in seats in good condition.</p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/01/29/image.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> seats = [["#",".","#","#",".","#"],
&nbsp;               [".","#","#","#","#","."],
&nbsp;               ["#",".","#","#",".","#"]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Teacher can place 4 students in available seats so they don't cheat on the exam. 
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> seats = [[".","#"],
&nbsp;               ["#","#"],
&nbsp;               ["#","."],
&nbsp;               ["#","#"],
&nbsp;               [".","#"]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Place all students in available seats. 

</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> seats = [["#",".","<strong>.</strong>",".","#"],
&nbsp;               ["<strong>.</strong>","#","<strong>.</strong>","#","<strong>.</strong>"],
&nbsp;               ["<strong>.</strong>",".","#",".","<strong>.</strong>"],
&nbsp;               ["<strong>.</strong>","#","<strong>.</strong>","#","<strong>.</strong>"],
&nbsp;               ["#",".","<strong>.</strong>",".","#"]]
<strong>Output:</strong> 10
<strong>Explanation:</strong> Place students in available seats in column 1, 3 and 5.
</pre>



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



<ul><li><code>seats</code>&nbsp;contains only characters&nbsp;<code>'.'&nbsp;and</code><code>'#'.</code></li><li><code>m ==&nbsp;seats.length</code></li><li><code>n ==&nbsp;seats[i].length</code></li><li><code>1 &lt;= m &lt;= 8</code></li><li><code>1 &lt;= n &lt;= 8</code></li></ul>



<h2><strong>Solution 1: DFS (TLE)</strong></h2>



<p>Time complexity: O(2^(m*n)) = O(2^64)<br>Space complexity: O(m*n)</p>



<h2><strong>Solution 2: DP</strong></h2>



<p>Since how to fill row[i+1] only depends on row[i]&#8217;s state, we can define</p>



<p>dp[i][s] as the max # of students after filling i rows and s (as a binary string) is the states i-th row.</p>



<p>dp[i+1][t] = max{dp[i][s] + bits(t)} if row[i] = s &amp;&amp; row[i +1] = t is a valid state.</p>



<p>Time complexity: O(m*2^(n+n)*n) = O(2^22)<br>Space complexity: O(m*2^n) = O(2^11) -&gt; O(2^n)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maxStudents(vector&lt;vector&lt;char&gt;&gt;&amp; seats) {
    int m = seats.size();
    int n = seats[0].size();
    vector&lt;vector&lt;int&gt;&gt; dp(m + 1, vector&lt;int&gt;(1 &lt;&lt; n));    
    for (int i = 0; i &lt; m; ++i)
      for (int l = 0; l &lt; (1 &lt;&lt; n); ++l)
        for (int c = 0; c &lt; (1 &lt;&lt; n); ++c) {
          bool flag = true;
          for (int j = 0; j &lt; n &amp;&amp; flag; ++j) {
            if (!(c &amp; (1 &lt;&lt; j))) continue;
            if (seats[i][j] == '#') flag = false;            
            bool l = j == 0 ? false : (c &amp; (1 &lt;&lt; (j - 1)));
            bool r = j == n - 1 ? false : (c &amp; (1 &lt;&lt; (j + 1)));
            bool ul = (j == 0 || i == 0) ? false : (l &amp; (1 &lt;&lt; (j - 1)));
            bool ur = (j == n - 1 || i == 0) ? false : (l &amp; (1 &lt;&lt; (j + 1)));
            if (l || r || ul || ur) flag = false;
          }
          if (flag)
            dp[i + 1][c] = max(dp[i + 1][c], dp[i][l] + __builtin_popcount(c));
        }
    return *max_element(begin(dp[m]), end(dp[m]));
  }
};</pre>
</div></div>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maxStudents(vector&lt;vector&lt;char&gt;&gt;&amp; seats) {
    const int m = seats.size();
    const int n = seats[0].size();
    vector&lt;int&gt; s(m + 1);    
    for (int i = 1; i &lt;= m; ++i)
      for (int j = 0; j &lt; n; ++j)
        s[i] |= (seats[i - 1][j] == '.') &lt;&lt; j;
    
    vector&lt;vector&lt;int&gt;&gt; dp(m + 1, vector&lt;int&gt;(1 &lt;&lt; n));        
    for (int i = 1; i &lt;= m; ++i)      
      for (int c = s[i];;c = (c - 1) &amp; s[i]) {
        if (c &amp; (c &gt;&gt; 1)) continue;
        for (int l = s[i - 1];;l = (l - 1) &amp; s[i - 1]) {
          if (!(l &amp; (c &gt;&gt; 1)) &amp;&amp; !((l &gt;&gt; 1) &amp; c))
            dp[i][c] = max(dp[i][c], dp[i - 1][l] + __builtin_popcount(c));          
          if (l == 0) break;
        }
        if (c == 0) break;
      }
    return *max_element(begin(dp[m]), end(dp[m]));
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1349-maximum-students-taking-exam/">花花酱 LeetCode 1349. Maximum Students Taking Exam</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/dynamic-programming/leetcode-1349-maximum-students-taking-exam/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
