{"id":6533,"date":"2020-03-22T13:34:07","date_gmt":"2020-03-22T20:34:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6533"},"modified":"2020-03-22T13:54:42","modified_gmt":"2020-03-22T20:54:42","slug":"leetcode-1386-cinema-seat-allocation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1386-cinema-seat-allocation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1386. Cinema Seat Allocation"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/02\/14\/cinema_seats_1.png\" alt=\"\"\/><\/figure>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/02\/14\/cinema_seats_3.png\" alt=\"\"\/><\/figure>\n\n\n\n<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]]\n<strong>Output:<\/strong> 4\n<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;\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 2, reservedSeats = [[2,1],[1,8],[2,6]]\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]]\n<strong>Output:<\/strong> 4\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: HashTable + Greedy<\/strong><\/h2>\n\n\n\n<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>\n\n\n\n<p>Time complexity: O(|reservedSeats|)<br>Space complexity: O(|rows|)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxNumberOfFamilies(int n, vector<vector<int>>& reservedSeats) {\n    unordered_map<int, int> rows;\n    for (auto& seat : reservedSeats)\n      rows[seat[0]] |= 1 << (seat[1] - 1);\n    \n    int ans = (n - rows.size()) * 2;\n    \n    for (const auto&#038; [idx, row] : rows) {\n      int s2 = row &#038; 0b0000011110;\n      int s4 = row &#038; 0b0001111000;\n      int s6 = row &#038; 0b0111100000;\n      if (s2 == 0 &#038;&#038; s6 == 0)\n        ans += 2;\n      else if (s2 == 0 || s4 == 0 || s6 == 0)\n        ans += 1;\n    }\n    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A cinema&nbsp;has&nbsp;n&nbsp;rows of seats, numbered from 1 to&nbsp;n&nbsp;and there are ten&nbsp;seats in each row, labelled from 1&nbsp;to 10&nbsp;as shown in the figure above. Given the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[549,16,284,88,82],"class_list":["post-6533","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-binary-mask","tag-bit","tag-geometry","tag-greedy","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6533"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6533\/revisions"}],"predecessor-version":[{"id":6538,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6533\/revisions\/6538"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}