파비의 매일매일 공부기록

Today's Challenge 본문

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]
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.04.26
Today's Challenge  (0) 2022.04.25
Today's Challenge  (0) 2022.04.23
Today's Challenge  (0) 2022.04.22
Today's Challenge  (0) 2022.04.21
Comments