-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC1396.cpp
More file actions
executable file
·43 lines (36 loc) · 1.33 KB
/
LC1396.cpp
File metadata and controls
executable file
·43 lines (36 loc) · 1.33 KB
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
36
37
38
39
40
41
42
43
/*
Problem Statement: https://leetcode.com/problems/design-underground-system/
Space: O(n² • len)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
|------------------------------------------|--------|--------|
| Operations | Time | Space |
|------------------------------------------|--------|--------|
| UndergroundSystem() | O(1) | O(1) |
| checkIn(id, stationName, t) | O(len) | O(len) |
| checkOut(id, stationName, t) | O(len) | O(len) |
| getAverageTime(startStation, endStation) | O(len) | O(len) |
|------------------------------------------|--------|--------|
*/
class UndergroundSystem {
private:
unordered_map<string, pair<int, int>> adj;
unordered_map<int, pair<string, int>> cust;
string convert(string& s1, string& s2) {
return s1 + "|" + s2;
}
public:
UndergroundSystem() {}
void checkIn(int id, string stationName, int t) {
cust[id] = {stationName, t};
}
void checkOut(int id, string stationName, int t) {
auto& [startStation, startTime] = cust[id];
pair<int, int>& p = adj[convert(startStation, stationName)];
p.first += t - startTime;
p.second++;
}
double getAverageTime(string startStation, string endStation) {
pair<int, int>& p = adj[convert(startStation, endStation)];
return (double) p.first / p.second;
}
};