-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo_1396.cs
83 lines (74 loc) · 2.74 KB
/
No_1396.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Diagnostics.Metrics;
namespace LeetCode
{
/// <summary>
/// Solucion al problema 1396 del portal LeetCode
/// </summary>
public class UndergroundSystem
{
List<Customer> _customers;
Dictionary<string, Dictionary<string, List<double>>> _stations;
public UndergroundSystem()
{
_customers = new List<Customer>();
_stations = new Dictionary<string, Dictionary<string, List<double>>>();
}
public void CheckIn(int id, string stationName, int t)
{
foreach (var cust in _customers)
{
if (cust.ID == id)
{
return;
}
}
_customers.Add(new Customer(stationName, id, t));
if (!_stations.ContainsKey(stationName))
_stations.Add(stationName, new Dictionary<string, List<double>>());
}
public void CheckOut(int id, string stationName, int t)
{
foreach (var cust in _customers)
{
if (cust.ID == id)
{
if (!_stations.ContainsKey(stationName))
_stations.Add(stationName, new Dictionary<string, List<double>>());
if (!_stations[cust.stationIn].ContainsKey(stationName))
{
_stations[cust.stationIn].Add(stationName, new List<double>());
_stations[cust.stationIn][stationName].AddRange(new List<double> { t - cust.timeIn, t - cust.timeIn });
}
else
{
_stations[cust.stationIn][stationName].Add(t - cust.timeIn);
var destinos = _stations[cust.stationIn][stationName];
double total = 0;
for (int i = 1; i < destinos.Count; i++)
total += destinos[i];
_stations[cust.stationIn][stationName][0] = total / (destinos.Count - 1);
}
Console.WriteLine(string.Join(',', _stations[cust.stationIn][stationName]));
_customers.Remove(cust);
break;
}
}
}
public double GetAverageTime(string startStation, string endStation)
{
return _stations[startStation][endStation][0];
}
}
partial class Customer
{
public string? stationIn { get; set; }
public int ID { get; set; }
public int timeIn { get; set; }
public Customer(string stationName, int id, int t)
{
stationIn = stationName;
ID = id;
timeIn = t;
}
}
}