-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1396_design_underground_system.rb
More file actions
41 lines (33 loc) · 971 Bytes
/
1396_design_underground_system.rb
File metadata and controls
41 lines (33 loc) · 971 Bytes
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
# frozen_string_literal: true
# https://leetcode.com/problems/design-underground-system/
class UndergroundSystem
# Init
def initialize
@stations = {}
@check_ins = {}
end
# @param {Integer} id
# @param {String} station_name
# @param {Integer} t
# @return {Void}
def check_in(id, station_name, t)
@check_ins[id] = { station_name: station_name, time: t }
end
# @param {Integer} id
# @param {String} station_name
# @param {Integer} t
# @return {Void}
def check_out(id, station_name, t)
check_in = @check_ins.delete(id)
name = "#{check_in[:station_name]} -> #{station_name}"
@stations[name] = [] unless @stations.include?(name)
@stations[name] << t - check_in[:time]
end
# @param {String} start_station
# @param {String} end_station
# @return {Float}
def get_average_time(start_station, end_station)
times = @stations["#{start_station} -> #{end_station}"]
times.sum.to_f / times.size
end
end