{"id":7439,"date":"2020-10-03T15:34:47","date_gmt":"2020-10-03T22:34:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7439"},"modified":"2020-10-03T15:35:38","modified_gmt":"2020-10-03T22:35:38","slug":"leetcode-1603-design-parking-system","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1603-design-parking-system\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1603. Design Parking System"},"content":{"rendered":"\n<p>Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.<\/p>\n\n\n\n<p>Implement the&nbsp;<code>ParkingSystem<\/code>&nbsp;class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>ParkingSystem(int big, int medium, int small)<\/code>&nbsp;Initializes object of the&nbsp;<code>ParkingSystem<\/code>&nbsp;class. The number of slots for each parking space are given as part of the constructor.<\/li><li><code>bool addCar(int carType)<\/code>&nbsp;Checks whether there is a parking space of&nbsp;<code>carType<\/code>&nbsp;for the car that wants to get into the parking lot.&nbsp;<code>carType<\/code>&nbsp;can be of three kinds: big, medium, or small, which are represented by&nbsp;<code>1<\/code>,&nbsp;<code>2<\/code>, and&nbsp;<code>3<\/code>&nbsp;respectively.&nbsp;<strong>A car can only park in a parking space of its&nbsp;<\/strong><code>carType<\/code>. If there is no space available, return&nbsp;<code>false<\/code>, else park the car in that size space and return&nbsp;<code>true<\/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[\"ParkingSystem\", \"addCar\", \"addCar\", \"addCar\", \"addCar\"]\n[[1, 1, 0], [1], [2], [3], [1]]\n<strong>Output<\/strong>\n[null, true, true, false, false]\n<\/pre>\n\n\n\n<p><strong>Explanation<\/strong>\nParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);\nparkingSystem.addCar(1); \/\/ return true because there is 1 available slot for a big car\nparkingSystem.addCar(2); \/\/ return true because there is 1 available slot for a medium car\nparkingSystem.addCar(3); \/\/ return false because there is no available slot for a small car\nparkingSystem.addCar(1); \/\/ return false because there is no available slot for a big car. It is already occupied.\n<\/p>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= big, medium, small &lt;= 1000<\/code><\/li><li><code>carType<\/code>&nbsp;is&nbsp;<code>1<\/code>,&nbsp;<code>2<\/code>, or&nbsp;<code>3<\/code><\/li><li>At most&nbsp;<code>1000<\/code>&nbsp;calls will be made to&nbsp;<code>addCar<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(1) per addCar call<br>Space complexity: O(1)<\/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 ParkingSystem {\npublic:\n  ParkingSystem(int big, int medium, int small): \n    slots_{{big, medium, small}} {}\n\n  bool addCar(int carType) {\n    return slots_[carType - 1]-- > 0;\n  }\nprivate:\n  vector<int> slots_;\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass ParkingSystem:\n  def __init__(self, big: int, medium: int, small: int):\n    self.slots = {1: big, 2: medium, 3: small}\n\n  def addCar(self, carType: int) -> bool:\n    if self.slots[carType] == 0: return False\n    self.slots[carType] -= 1\n    return True\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[369,251,222,82,179],"class_list":["post-7439","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-class","tag-design","tag-easy","tag-hashtable","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7439","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=7439"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7439\/revisions"}],"predecessor-version":[{"id":7441,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7439\/revisions\/7441"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}