|
| 1 | +/** |
| 2 | + * 1603. Design Parking System |
| 3 | + * https://leetcode.com/problems/design-parking-system/ |
| 4 | + * Difficulty: Easy |
| 5 | + * |
| 6 | + * Design a parking system for a parking lot. The parking lot has three kinds of parking |
| 7 | + * spaces: big, medium, and small, with a fixed number of slots for each size. |
| 8 | + * |
| 9 | + * Implement the ParkingSystem class: |
| 10 | + * - ParkingSystem(int big, int medium, int small) Initializes object of the ParkingSystem class. |
| 11 | + * The number of slots for each parking space are given as part of the constructor. |
| 12 | + * - bool addCar(int carType) Checks whether there is a parking space of carType for the car that |
| 13 | + * wants to get into the parking lot. carType can be of three kinds: big, medium, or small, which |
| 14 | + * are represented by 1, 2, and 3 respectively. A car can only park in a parking space of its |
| 15 | + * carType. If there is no space available, return false, else park the car in that size space |
| 16 | + * and return true. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * @param {number} big |
| 21 | + * @param {number} medium |
| 22 | + * @param {number} small |
| 23 | + */ |
| 24 | +var ParkingSystem = function(big, medium, small) { |
| 25 | + this.spaces = { 1: big, 2: medium, 3: small }; |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * @param {number} carType |
| 30 | + * @return {boolean} |
| 31 | + */ |
| 32 | +ParkingSystem.prototype.addCar = function(carType) { |
| 33 | + if (this.spaces[carType] > 0) { |
| 34 | + this.spaces[carType]--; |
| 35 | + return true; |
| 36 | + } |
| 37 | + return false; |
| 38 | +}; |
0 commit comments