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