Problem solving/Algorithms

[LeetCode] 1603. Design Parking System (Python)

Young_A 2021. 1. 10. 09:50

๋ชฉ์ฐจ

    LeetCode - Problems - Algorithms - 1603. Design Parking System

    Problem Description

    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) Initialized 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 123 respectibely. A car can only part 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.

    Example:

    Constraints:

    My Solution (Python)

    class ParkingSystem(object):
    
        def __init__(self, big, medium, small):
            """
            :type big: int
            :type medium: int
            :type small: int
            """
            self.big = big
            self.medium = medium
            self.small = small
            
    
        def addCar(self, carType):
            """
            :type carType: int
            :rtype: bool
            """
            if carType == 1:
                if self.big <= 0:
                    return False
                else:
                    self.big -= 1
                    return True
            elif carType == 2:
                if self.medium <= 0:
                    return False
                else:
                    self.medium -= 1
                    return True
            elif carType == 3:
                if self.small <= 0:
                    return False
                else:
                    self.small -= 1
                    return True