반응형
Problem
이번 문제는 좀 길게 나왔다.
하지만 문제는 간단한 편이다 . 주차시스템이있고 [1,1,0] 앞에서부터 big, medium,small 의 차량이 들어갈 수있는 수가 담긴 배열이 있고
addcar로 각 사이즈별 1 = big, 2 = medium, 3 = small 의 차량을 주차시키고 True를 반환하고
차량을 주차시킬수 있는 칸이없으면 False를 반환한다
나의 풀이
class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
self.park = [big,medium, small]
def addCar(self, carType: int) -> bool:
carType -= 1
if self.park[carType] > 0:
self.park[carType] -= 1
else:
return False
return True
if 문이 많아지는 것보다 간단하게 작성할 수 있는 방법이 없는지 고민하다가
park 라는 배열을 만들어서 index로 구분을 하여 문제를 풀어냈다 .
자 다른 사람의 풀이를 보자!
class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
self.cars = {1: big, 2: medium, 3: small}
def addCar(self, carType: int) -> bool:
self.cars[carType] -= 1
return self.cars[carType] + 1 > 0
이 사람은 dictionary를 사용하여 문제를 풀어 냈다.
key값을 줘서 좀 더 간단하게 작성을 햇고
주차장의 분류가 세분화되고 커진다면 이 분의 풀이가 더 좋을듯 하다
반응형
'Python > algorithm' 카테고리의 다른 글
[Leetcode] 1431. Kids With the Greatest Number of Candies (1) | 2023.01.02 |
---|---|
[Leetcode] 2413. Smallest Even Multiple (1) | 2022.12.31 |
[Leetcode] 2114. Maximum Number of Words Found in Sentences (0) | 2022.12.29 |
[Leetcode] 771. Jewels and Stones (0) | 2022.12.27 |
[Leetcode] 1672. Richest Customer WealthEasy2.9K305 (0) | 2022.12.26 |