euisblue
1603. Design Parking System

C++

1/** 2 * Your ParkingSystem object will be instantiated and called as such: 3 * ParkingSystem* obj = new ParkingSystem(big, medium, small); 4 * bool param_1 = obj->addCar(carType); 5 * 6 * Runtime: 84 ms, faster than 90.67% 7 * Memory Usage: 33.5 MB, less than 7.82% 8 */ 9class ParkingSystem { 10 private: 11 int carCapacity[3]; 12 int cnt[3]; 13 public: 14 ParkingSystem(int big, int medium, int small) { 15 carCapacity[0] = big; 16 carCapacity[1] = medium; 17 carCapacity[2] = small; 18 19 cnt[0] = cnt[1] = cnt[2] = 0; 20 } 21 22 bool addCar(int carType) { 23 return carCapacity[carType-1] > (cnt[carType-1])++; 24 } 25};

Ruby

1# Runtime: 88 ms, faster than 74.36% 2# Memory Usage: 210.5 MB, less than 6.41% 3# 4# Your ParkingSystem object will be instantiated and called as such: 5# obj = ParkingSystem.new(big, medium, small) 6# param_1 = obj.add_car(car_type) 7class ParkingSystem 8 attr_accessor :cnt, :cntCapacity 9 10=begin 11 :type big: Integer 12 :type medium: Integer 13 :type small: Integer 14=end 15 def initialize(big, medium, small) 16 @cntCapacity = [big, medium, small] 17 @cnt = [0, 0, 0] 18 end 19 20=begin 21 :type car_type: Integer 22 :rtype: Boolean 23=end 24 def add_car(car_type) 25 if @cnt[car_type-1] >= @cntCapacity[car_type-1] 26 return false 27 else 28 @cnt[car_type-1] += 1 29 return true 30 end 31 end 32end

JavaScript

1/** 2 * Runtime: 156 ms, faster than 37.73% 3 * Memory Usage: 46.8 MB, less than 20.10% 4 */ 5 6let cntCapacity; 7let cnt; 8 9/** 10 * @param {number} big 11 * @param {number} medium 12 * @param {number} small 13 */ 14var ParkingSystem = function(big, medium, small) { 15 cntCapacity = [big, medium, small]; 16 cnt = [0, 0, 0]; 17}; 18 19/** 20 * @param {number} carType 21 * @return {boolean} 22 */ 23ParkingSystem.prototype.addCar = function(carType) { 24 if (cntCapacity[carType-1] == cnt[carType-1]) return false; 25 else { 26 cnt[carType-1] += 1; 27 return true; 28 } 29}; 30 31/** 32 * Your ParkingSystem object will be instantiated and called as such: 33 * var obj = new ParkingSystem(big, medium, small) 34 * var param_1 = obj.addCar(carType) 35 */