{"id":8409,"date":"2021-05-01T15:28:54","date_gmt":"2021-05-01T22:28:54","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8409"},"modified":"2021-05-01T15:29:19","modified_gmt":"2021-05-01T22:29:19","slug":"leetcode-1845-seat-reservation-manager","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/data-structure\/leetcode-1845-seat-reservation-manager\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1845. Seat Reservation Manager"},"content":{"rendered":"\n<p>Design a system that manages the reservation state of&nbsp;<code>n<\/code>&nbsp;seats that are numbered from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<p>Implement the&nbsp;<code>SeatManager<\/code>&nbsp;class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>SeatManager(int n)<\/code>&nbsp;Initializes a&nbsp;<code>SeatManager<\/code>&nbsp;object that will manage&nbsp;<code>n<\/code>&nbsp;seats numbered from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>. All seats are initially available.<\/li><li><code>int reserve()<\/code>&nbsp;Fetches the&nbsp;<strong>smallest-numbered<\/strong>&nbsp;unreserved seat, reserves it, and returns its number.<\/li><li><code>void unreserve(int seatNumber)<\/code>&nbsp;Unreserves the seat with the given&nbsp;<code>seatNumber<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input<\/strong>\n[\"SeatManager\", \"reserve\", \"reserve\", \"unreserve\", \"reserve\", \"reserve\", \"reserve\", \"reserve\", \"unreserve\"]\n[[5], [], [], [2], [], [], [], [], [5]]\n<strong>Output<\/strong>\n\n[null, 1, 2, null, 2, 3, 4, 5, null]\n\n<strong>Explanation<\/strong> SeatManager seatManager = new SeatManager(5); \/\/ Initializes a SeatManager with 5 seats. seatManager.reserve(); \/\/ All seats are available, so return the lowest numbered seat, which is 1. seatManager.reserve(); \/\/ The available seats are [2,3,4,5], so return the lowest of them, which is 2. seatManager.unreserve(2); \/\/ Unreserve seat 2, so now the available seats are [2,3,4,5]. seatManager.reserve(); \/\/ The available seats are [2,3,4,5], so return the lowest of them, which is 2. seatManager.reserve(); \/\/ The available seats are [3,4,5], so return the lowest of them, which is 3. seatManager.reserve(); \/\/ The available seats are [4,5], so return the lowest of them, which is 4. seatManager.reserve(); \/\/ The only available seat is seat 5, so return 5. seatManager.unreserve(5); \/\/ Unreserve seat 5, so now the available seats are [5].\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<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= seatNumber &lt;= n<\/code><\/li><li>For each call to&nbsp;<code>reserve<\/code>, it is guaranteed that there will be at least one unreserved seat.<\/li><li>For each call to&nbsp;<code>unreserve<\/code>, it is guaranteed that&nbsp;<code>seatNumber<\/code>&nbsp;will be reserved.<\/li><li>At most&nbsp;<code>10<sup>5<\/sup><\/code>&nbsp;calls&nbsp;<strong>in total<\/strong>&nbsp;will be made to&nbsp;<code>reserve<\/code>&nbsp;and&nbsp;<code>unreserve<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: TreeSet<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/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 SeatManager {\npublic:\n  SeatManager(int n) {\n    for (int i = 1; i <= n; ++i)\n      s_.insert(i);\n  }\n\n  int reserve() {    \n    int seat = *begin(s_);\n    s_.erase(begin(s_));    \n    return seat;\n  }\n\n  void unreserve(int seatNumber) {\n    s_.insert(seatNumber);    \n  }\nprivate:\n  set<int> s_;\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Design a system that manages the reservation state of&nbsp;n&nbsp;seats that are numbered from&nbsp;1&nbsp;to&nbsp;n. Implement the&nbsp;SeatManager&nbsp;class: SeatManager(int n)&nbsp;Initializes a&nbsp;SeatManager&nbsp;object that will manage&nbsp;n&nbsp;seats numbered from&nbsp;1&nbsp;to&nbsp;n. All seats&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89],"tags":[291,177,656],"class_list":["post-8409","post","type-post","status-publish","format-standard","hentry","category-data-structure","tag-data-structure","tag-medium","tag-treeset","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8409","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=8409"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8409\/revisions"}],"predecessor-version":[{"id":8413,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8409\/revisions\/8413"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}