Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions csharp/1396-design-underground-system.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class UndergroundSystem
{
// key: id
// value: station, time
private Dictionary<int, (string, int)> 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;
}
}