[Python] 1603. Design Parking System

정지은·2022년 10월 12일
0

코딩문제

목록 보기
19/25

1603. Design Parking System

문제

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.

Implement the ParkingSystem class:

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.

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.

https://leetcode.com/problems/design-parking-system/

접근

#디자인 #리스트

주차공간의 정보를 담은 리스트를 선언하고, addCar가 선언될 때마다 해당하는 값을 하나씩 줄인다. 그 결과가 0보다 작으면 False, 같거나 크면 True를 리턴한다.

코드

class ParkingSystem:
    
    def __init__(self, big: int, medium: int, small: int):
        self.parking = [0, big, medium, small]

    def addCar(self, carType: int) -> bool:
        if self.parking[carType] - 1 < 0:
            return False
        else:
            self.parking[carType] -= 1
            return True


# Your ParkingSystem object will be instantiated and called as such:
# obj = ParkingSystem(big, medium, small)
# param_1 = obj.addCar(carType)

효율성

Runtime: 196 ms, faster than 70.22% of Python3 online submissions for Design Parking System.
Memory Usage: 14.5 MB, less than 62.80% of Python3 online submissions for Design Parking System.

profile
Steady!

1개의 댓글

comment-user-thumbnail
2023년 9월 19일

I appreciate your article on designing a parking system using Python. It's a valuable resource for developers interested in the technical aspects of such systems. I'd also like to share another insightful article on parking app development by Cleveroad (https://www.cleveroad.com/blog/parking-app-development/).

답글 달기