File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
08_greedy_algorithms/ruby Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ require "set"
2
+
3
+ # You pass an array in, and it gets converted to a Set.new.
4
+ states_needed = Set . new ( %w( mt wa or id nv ut ca az ) )
5
+
6
+ stations = { }
7
+ stations [ "kone" ] = Set . new ( %w( id nv ut ) )
8
+ stations [ "ktwo" ] = Set . new ( %w( wa id mt ) )
9
+ stations [ "kthree" ] = Set . new ( %w( or nv ca ) )
10
+ stations [ "kfour" ] = Set . new ( %w( nv ut ) )
11
+ stations [ "kfive" ] = Set . new ( %w( ca az ) )
12
+
13
+ final_stations = Set . new
14
+
15
+ until states_needed . empty?
16
+ best_station = nil
17
+ states_covered = Set . new
18
+
19
+ stations . each do |station , states |
20
+ covered = states_needed & states
21
+ if covered . length > states_covered . length
22
+ best_station = station
23
+ states_covered = covered
24
+ end
25
+ end
26
+
27
+ states_needed -= states_covered
28
+ final_stations << best_station
29
+ end
30
+
31
+ p final_stations
You can’t perform that action at this time.
0 commit comments