Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 1382. Balance a Binary Search Tree | [Link](https://leetcode.com/problems/balance-a-binary-search-tree/) | [Link](./lib/medium/1382_balance_a_binary_search_tree.rb) | [Link](./test/medium/test_1382_balance_a_binary_search_tree.rb) |
| 1387. Sort Integers by The Power Value | [Link](https://leetcode.com/problems/sort-integers-by-the-power-value/) | [Link](./lib/medium/1387_sort_integers_by_the_power_value.rb) | [Link](./test/medium/test_1387_sort_integers_by_the_power_value.rb) |
| 1395. Count Number of Teams | [Link](https://leetcode.com/problems/count-number-of-teams/) | [Link](./lib/medium/1395_count_number_of_teams.rb) | [Link](./test/medium/test_1395_count_number_of_teams.rb) |
| 1396. Design Underground System | [Link](https://leetcode.com/problems/design-underground-system/) | [Link](./lib/medium/1396_design_underground_system.rb) | [Link](./test/medium/test_1396_design_underground_system.rb) |
| 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) |
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
Expand Down
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '8.5.7'
s.version = '8.5.8'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
41 changes: 41 additions & 0 deletions lib/medium/1396_design_underground_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
92 changes: 92 additions & 0 deletions test/medium/test_1396_design_underground_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/1396_design_underground_system'
require 'minitest/autorun'

class DesignUndergroundSystemTest < ::Minitest::Test
def test_default_one
underground_system = ::UndergroundSystem.new

underground_system.check_in(45, 'Leyton', 3)
underground_system.check_in(32, 'Paradise', 8)
underground_system.check_in(27, 'Leyton', 10)

underground_system.check_out(45, 'Waterloo', 15)
underground_system.check_out(27, 'Waterloo', 20)
underground_system.check_out(32, 'Cambridge', 22)

assert_in_delta(
14.00000,
underground_system.get_average_time(
'Paradise',
'Cambridge'
)
)

assert_in_delta(
11.00000,
underground_system.get_average_time(
'Leyton',
'Waterloo'
)
)

underground_system.check_in(10, 'Leyton', 24)

assert_in_delta(
11.00000,
underground_system.get_average_time(
'Leyton',
'Waterloo'
)
)

underground_system.check_out(10, 'Waterloo', 38)

assert_in_delta(
12.00000,
underground_system.get_average_time(
'Leyton',
'Waterloo'
)
)
end

def test_default_two
underground_system = ::UndergroundSystem.new

underground_system.check_in(10, 'Leyton', 3)
underground_system.check_out(10, 'Paradise', 8)

assert_in_delta(
5.00000,
underground_system.get_average_time(
'Leyton',
'Paradise'
)
)

underground_system.check_in(5, 'Leyton', 10)
underground_system.check_out(5, 'Paradise', 16)

assert_in_delta(
5.50000,
underground_system.get_average_time(
'Leyton',
'Paradise'
)
)

underground_system.check_in(2, 'Leyton', 21)
underground_system.check_out(2, 'Paradise', 30)

assert_in_delta(
6.66667,
underground_system.get_average_time(
'Leyton',
'Paradise'
)
)
end
end