|
| 1 | +// https://leetcode.com/problems/design-parking-system |
| 2 | +// |
| 3 | +// 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. |
| 4 | +// |
| 5 | +// Implement the `ParkingSystem` class: |
| 6 | +// |
| 7 | +// * `ParkingSystem(int big, int medium, int small)` Initializes object of the `ParkingSystem` class. The number of slots for each parking space are given as part of the constructor. |
| 8 | +// * `bool addCar(int carType)` Checks whether there is a parking space of `carType` for the car that wants to get into the parking lot. `carType` can be of three kinds: big, medium, or small, which are represented by `1`, `2`, and `3` respectively. **A car can only park in a parking space of its** `carType`. If there is no space available, return `false`, else park the car in that size space and return `true`. |
| 9 | +// |
| 10 | +// **Example 1:** |
| 11 | +// |
| 12 | +// ``` |
| 13 | +// **Input** |
| 14 | +// ["ParkingSystem", "addCar", "addCar", "addCar", "addCar"] |
| 15 | +// [[1, 1, 0], [1], [2], [3], [1]] |
| 16 | +// **Output** |
| 17 | +// [null, true, true, false, false] |
| 18 | +// |
| 19 | +// **Explanation** |
| 20 | +// ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0); |
| 21 | +// parkingSystem.addCar(1); // return true because there is 1 available slot for a big car |
| 22 | +// parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car |
| 23 | +// parkingSystem.addCar(3); // return false because there is no available slot for a small car |
| 24 | +// parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied. |
| 25 | +// ``` |
| 26 | +// |
| 27 | +// **Constraints:** |
| 28 | +// |
| 29 | +// * `0 <= big, medium, small <= 1000` |
| 30 | +// * `carType` is `1`, `2`, or `3` |
| 31 | +// * At most `1000` calls will be made to `addCar` |
| 32 | + |
| 33 | +struct ParkingSystem { |
| 34 | + big: i32, |
| 35 | + medium: i32, |
| 36 | + small: i32, |
| 37 | +} |
| 38 | + |
| 39 | +impl ParkingSystem { |
| 40 | + |
| 41 | + fn new(big: i32, medium: i32, small: i32) -> Self { |
| 42 | + ParkingSystem { |
| 43 | + big, |
| 44 | + medium, |
| 45 | + small, |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + fn add_car(&mut self, car_type: i32) -> bool { |
| 50 | + match car_type { |
| 51 | + 1 => { |
| 52 | + self.big = self.big - 1; |
| 53 | + self.big >= 0 |
| 54 | + }, |
| 55 | + 2 => { |
| 56 | + self.medium = self.medium -1; |
| 57 | + self.medium >= 0 |
| 58 | + }, |
| 59 | + 3 => { |
| 60 | + self.small = self.small - 1; |
| 61 | + self.small >= 0 |
| 62 | + }, |
| 63 | + _ => false, |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[test] |
| 69 | +pub fn t1() { |
| 70 | + let mut parking_system = ParkingSystem::new(1, 1, 0); |
| 71 | + assert_eq!(parking_system.add_car(1), true); |
| 72 | + assert_eq!(parking_system.add_car(2), true); |
| 73 | + assert_eq!(parking_system.add_car(3), false); |
| 74 | + assert_eq!(parking_system.add_car(1), false); |
| 75 | +} |
0 commit comments