Post

leetcode(리트코드)3월20일 challenge1396-Design Underground System

leetcode March 20일 - Design Underground System 문제입니다.

1. 문제

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


2. Input , Output


3. 분류 및 난이도

Medium 난이도입니다.
3월 20일자 챌린지 문제입니다.


4. 문제 해석

  • checkIn으로 고유한 ID, 시작 장소, 도착한 시간이 들어옵니다.
  • checkout으로 고유한 ID, 끝나는 장소, 나간시간이 들어옵니다.
  • getAverageTime으로 시작한 장소와 끝나는 장소가 들어오고 그 장소에서 사람들이 평균적으로 머무른 시간 (나간시간 - 도착한시간) / 사람수 를 리턴합니다.
  • map2개와 pair 2개를 써서 관리하였습니다.

5. code

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class UndergroundSystem {
    //stationName, id, time
    unordered_map<int,pair<string,int>> inmm;
    unordered_map<string,pair<int,int>> outmm;
public:
    UndergroundSystem() {
        
    }
    
    void checkIn(int id, string stationName, int t) {
        inmm[id]= {stationName , t};
    }
    
    void checkOut(int id, string stationName, int t) {
        auto& it = inmm[id];
        string end = it.first + "-" + stationName;
        outmm[end].first += t - it.second;
        outmm[end].second +=1;
        
    }
    
    double getAverageTime(string startStation, string endStation) {
        string find = startStation + "-" + endStation;
        double result = double(outmm[find].first) / double(outmm[find].second);
        return result;
    }
};

/**
 * Your UndergroundSystem object will be instantiated and called as such:
 * UndergroundSystem* obj = new UndergroundSystem();
 * obj->checkIn(id,stationName,t);
 * obj->checkOut(id,stationName,t);
 * double param_3 = obj->getAverageTime(startStation,endStation);
 */

6. 결과 및 후기, 개선점

c++ 78%

This post is licensed under CC BY 4.0 by the author.