Skip to content

Commit 94c6be7

Browse files
committed
1396. Design Underground System
1 parent 000c6e7 commit 94c6be7

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@
258258
| 1137 | N-th Tribonacci Number | [Ruby](./algorithms/ruby/1137-n-th-tribonacci-number.rb) | Easy |
259259
| 1372 | Longest ZigZag Path in a Binary Tree | [Ruby](./algorithms/ruby/1372-longest-zigzag-path-in-a-binary-tree.rb) | Medium |
260260
| 1345 | Jump Game IV | [Ruby](./algorithms/ruby/1345-jump-game-iv.rb) | Hard |
261+
| 1396 | Design Underground System | [Ruby](./algorithms/ruby/1396-design-underground-system.rb) | Medium |
261262
| 1402 | Reducing Dishes | [Ruby](./algorithms/ruby/1402-reducing-dishes.rb) | Hard |
262263
| 1406 | Stone Game III | [Ruby](./algorithms/ruby/1406-stone-game-iii.rb) | Hard |
263264
| 1416 | Restore The Array | [Ruby](./algorithms/ruby/1416-restore-the-array.rb) | Hard |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# frozen_string_literal: true
2+
3+
# 1396. Design Underground System
4+
# https://leetcode.com/problems/design-underground-system
5+
# Medium
6+
7+
=begin
8+
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another.
9+
10+
Implement the UndergroundSystem class:
11+
12+
* void checkIn(int id, string stationName, int t)
13+
* A customer with a card ID equal to id, checks in at the station stationName at time t.
14+
* A customer can only be checked into one place at a time.
15+
* void checkOut(int id, string stationName, int t)
16+
* A customer with a card ID equal to id, checks out from the station stationName at time t.
17+
* double getAverageTime(string startStation, string endStation)
18+
* Returns the average time it takes to travel from startStation to endStation.
19+
* The average time is computed from all the previous traveling times from startStation to endStation that happened directly, meaning a check in at startStation followed by a check out from endStation.
20+
* The time it takes to travel from startStation to endStation may be different from the time it takes to travel from endStation to startStation.
21+
* There will be at least one customer that has traveled from startStation to endStation before getAverageTime is called.
22+
You may assume all calls to the checkIn and checkOut methods are consistent. If a customer checks in at time t1 then checks out at time t2, then t1 < t2. All events happen in chronological order.
23+
24+
Example 1:
25+
Input
26+
["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"]
27+
[[],[45,"Leyton",3],[32,"Paradise",8],[27,"Leyton",10],[45,"Waterloo",15],[27,"Waterloo",20],[32,"Cambridge",22],["Paradise","Cambridge"],["Leyton","Waterloo"],[10,"Leyton",24],["Leyton","Waterloo"],[10,"Waterloo",38],["Leyton","Waterloo"]]
28+
Output
29+
[null,null,null,null,null,null,null,14.00000,11.00000,null,11.00000,null,12.00000]
30+
Explanation
31+
UndergroundSystem undergroundSystem = new UndergroundSystem();
32+
undergroundSystem.checkIn(45, "Leyton", 3);
33+
undergroundSystem.checkIn(32, "Paradise", 8);
34+
undergroundSystem.checkIn(27, "Leyton", 10);
35+
undergroundSystem.checkOut(45, "Waterloo", 15); // Customer 45 "Leyton" -> "Waterloo" in 15-3 = 12
36+
undergroundSystem.checkOut(27, "Waterloo", 20); // Customer 27 "Leyton" -> "Waterloo" in 20-10 = 10
37+
undergroundSystem.checkOut(32, "Cambridge", 22); // Customer 32 "Paradise" -> "Cambridge" in 22-8 = 14
38+
undergroundSystem.getAverageTime("Paradise", "Cambridge"); // return 14.00000. One trip "Paradise" -> "Cambridge", (14) / 1 = 14
39+
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.00000. Two trips "Leyton" -> "Waterloo", (10 + 12) / 2 = 11
40+
undergroundSystem.checkIn(10, "Leyton", 24);
41+
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.00000
42+
undergroundSystem.checkOut(10, "Waterloo", 38); // Customer 10 "Leyton" -> "Waterloo" in 38-24 = 14
43+
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 12.00000. Three trips "Leyton" -> "Waterloo", (10 + 12 + 14) / 3 = 12
44+
45+
Example 2:
46+
Input
47+
["UndergroundSystem","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime"]
48+
[[],[10,"Leyton",3],[10,"Paradise",8],["Leyton","Paradise"],[5,"Leyton",10],[5,"Paradise",16],["Leyton","Paradise"],[2,"Leyton",21],[2,"Paradise",30],["Leyton","Paradise"]]
49+
Output
50+
[null,null,null,5.00000,null,null,5.50000,null,null,6.66667]
51+
Explanation
52+
UndergroundSystem undergroundSystem = new UndergroundSystem();
53+
undergroundSystem.checkIn(10, "Leyton", 3);
54+
undergroundSystem.checkOut(10, "Paradise", 8); // Customer 10 "Leyton" -> "Paradise" in 8-3 = 5
55+
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.00000, (5) / 1 = 5
56+
undergroundSystem.checkIn(5, "Leyton", 10);
57+
undergroundSystem.checkOut(5, "Paradise", 16); // Customer 5 "Leyton" -> "Paradise" in 16-10 = 6
58+
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.50000, (5 + 6) / 2 = 5.5
59+
undergroundSystem.checkIn(2, "Leyton", 21);
60+
undergroundSystem.checkOut(2, "Paradise", 30); // Customer 2 "Leyton" -> "Paradise" in 30-21 = 9
61+
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 6.66667, (5 + 6 + 9) / 3 = 6.66667
62+
63+
Constraints:
64+
* 1 <= id, t <= 106
65+
* 1 <= stationName.length, startStation.length, endStation.length <= 10
66+
* All strings consist of uppercase and lowercase English letters and digits.
67+
* There will be at most 2 * 104 calls in total to checkIn, checkOut, and getAverageTime.
68+
* Answers within 10-5 of the actual value will be accepted.
69+
=end
70+
71+
class UndergroundSystem
72+
def initialize
73+
@check = {}
74+
@time = {}
75+
end
76+
77+
=begin
78+
:type id: Integer
79+
:type station_name: String
80+
:type t: Integer
81+
:rtype: Void
82+
=end
83+
def check_in(id, station_name, t)
84+
@check[id] = [station_name, t]
85+
end
86+
87+
88+
=begin
89+
:type id: Integer
90+
:type station_name: String
91+
:type t: Integer
92+
:rtype: Void
93+
=end
94+
def check_out(id, station_name, t)
95+
station_prev, t_prev = @check[id]
96+
t_diff = t - t_prev
97+
98+
if @time[[station_prev, station_name]]
99+
total_time, station_i = @time[[station_prev, station_name]]
100+
@time[[station_prev, station_name]] = [total_time + t_diff, station_i + 1]
101+
else
102+
@time[[station_prev, station_name]] = [t_diff, 1]
103+
end
104+
end
105+
106+
107+
=begin
108+
:type start_station: String
109+
:type end_station: String
110+
:rtype: Float
111+
=end
112+
def get_average_time(start_station, end_station)
113+
total_time, station_i = @time[[start_station, end_station]]
114+
total_time.to_f / station_i
115+
end
116+
end
117+
118+
# Your UndergroundSystem object will be instantiated and called as such:
119+
# obj = UndergroundSystem.new()
120+
# obj.check_in(id, station_name, t)
121+
# obj.check_out(id, station_name, t)
122+
# param_3 = obj.get_average_time(start_station, end_station)

0 commit comments

Comments
 (0)