Problem Solving/LeetCode
Today's Challenge
fabichoi
2022. 4. 24. 23:45
https://leetcode.com/problems/design-underground-system
Design Underground System - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
구현 문제는 처음으로 본 듯.
dictionary 써서 풀면 간단히 구현됨.
다만, 평균을 구하기 위해 모든 구간을 반복하는 게 아닌 통계 테이블처럼 따로 dict에 저장 후 연산
class UndergroundSystem:
def __init__(self):
self.trip = {}
self.trips = {}
def checkIn(self, id: int, stationName: str, t: int) -> None:
self.trip[id] = [stationName, t]
def checkOut(self, id: int, stationName: str, t: int) -> None:
s_station, s_t = self.trip.pop(id)
trip = (s_station, stationName)
t_time = t - s_t
if trip not in self.trips:
self.trips[trip] = [t_time, 1]
else:
total_t, times = self.trips[trip]
self.trips[trip] = [total_t + t_time, times + 1.0]
def getAverageTime(self, startStation: str, endStation: str) -> float:
return self.trips[(startStation, endStation)][0] / self.trips[(startStation, endStation)][1]
반응형