diff --git a/csharp/1396-design-underground-system.cs b/csharp/1396-design-underground-system.cs new file mode 100644 index 000000000..d69635922 --- /dev/null +++ b/csharp/1396-design-underground-system.cs @@ -0,0 +1,31 @@ +public class UndergroundSystem +{ + // key: id + // value: station, time + private Dictionary checked_in = new(); + // key: start and end station + // value: total time, count + private Dictionary<(string, string), (int, int)> backlog = new(); + public UndergroundSystem() { } + + public void CheckIn(int id, string stationName, int t) + { + checked_in[id] = (stationName, t); + } + + public void CheckOut(int id, string stationName, int t) + { + (string station, int time) = checked_in[id]; + if (!backlog.TryAdd((station, stationName), (t - time, 1))) + { + (int total_time, int count) = backlog[(station, stationName)]; + backlog[(station, stationName)] = (total_time + (t - time), count + 1); + } + } + + public double GetAverageTime(string startStation, string endStation) + { + (int total_time, int count) = backlog[(startStation, endStation)]; + return (double)total_time / count; + } +} \ No newline at end of file