-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy path1396-DesignUndergroundSystem.cs
57 lines (49 loc) · 1.87 KB
/
1396-DesignUndergroundSystem.cs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//-----------------------------------------------------------------------------
// Runtime: 624ms
// Memory Usage: 47.5 MB
// Link: https://leetcode.com/submissions/detail/368937157/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _1396_DesignUndergroundSystem
{
private IDictionary<string, (int sum, int times)> completes;
private IDictionary<int, (string start, int startTime)> checkIn;
public _1396_DesignUndergroundSystem()
{
completes = new Dictionary<string, (int sum, int times)>();
checkIn = new Dictionary<int, (string start, int startTime)>();
}
public void CheckIn(int id, string stationName, int t)
{
checkIn.Add(id, (stationName, t));
}
public void CheckOut(int id, string stationName, int t)
{
(string start, int startTime) = checkIn[id];
checkIn.Remove(id);
var route = $"{start}_{stationName}";
if (!completes.ContainsKey(route))
completes[route] = (t - startTime, 1);
else
{
(int sum, int times) = completes[route];
completes[route] = (sum + (t - startTime), times + 1);
}
}
public double GetAverageTime(string startStation, string endStation)
{
var route = $"{startStation}_{endStation}";
(int sum, int times) = completes[route];
return 1.0 * sum / times;
}
}
/**
* 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);
*/
}