파비의 매일매일 공부기록

2023.05.31 Today's Challenge 본문

Problem Solving/LeetCode

2023.05.31 Today's Challenge

fabichoi 2023. 5. 31. 23:45

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

 

Design Underground System - LeetCode

Can you solve this real interview question? Design Underground System - An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one s

leetcode.com

소스가 꽤 김

class UndergroundSystem:
    def __init__(self):
        self.tt = {}
        self.ci = {}

    def checkIn(self, id: int, stationName: str, t: int) -> None:
        self.ci[id] = (stationName, t)

    def checkOut(self, id: int, stationName: str, t: int) -> None:
        ss, cit = self.ci.pop(id)
        travel = (ss, stationName)
        travel_time = t - cit

        if travel in self.tt:
            total_time, count = self.tt[travel]
            self.tt[travel] = (total_time + travel_time, count+1)
        else:
            self.tt[travel] = (travel_time, 1)
        
    def getAverageTime(self, startStation: str, endStation: str) -> float:
        travel = (startStation, endStation)
        total_time, count = self.tt[travel]
        return total_time / count
반응형

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

2023.06.02 Today's Challenge  (0) 2023.06.02
2023.06.01 Today's Challenge  (0) 2023.06.01
2023.05.30 Today's Challenge  (0) 2023.05.30
2023.05.29 Today's Challenge  (0) 2023.05.29
2023.05.28 Today's Challenge  (0) 2023.05.28
Comments