<?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>state Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/state/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/state/</link>
	<description></description>
	<lastBuildDate>Wed, 12 Feb 2020 06:22:24 +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>state Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/state/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
