reorg and add code for second edition

This commit is contained in:
Aditya Bhargava
2023-08-09 08:20:19 -05:00
parent 9306432a1b
commit 933acafaf3
89 changed files with 18 additions and 117 deletions

View File

@@ -0,0 +1,35 @@
import Foundation
// You pass an array in, and it gets converted to a set.
var statesNeeded : Set = ["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]
var stations = [String: Set<String>]()
stations["kone"] = ["id", "nv", "ut"]
stations["ktwo"] = ["wa", "id", "mt"]
stations["kthree"] = ["or", "nv", "ca"]
stations["kfour"] = ["nv", "ut"]
stations["kfive"] = ["ca", "az"]
var finalStations = Set<String>();
while !statesNeeded.isEmpty {
var bestStation = String()
var statesCovered = Set<String>()
for station in stations {
var covered = statesNeeded.intersection(station.value)
if covered.count > statesCovered.count {
bestStation = station.key
statesCovered = covered
}
statesNeeded = statesNeeded.subtracting(statesCovered)
//Swift note: We should avoid adding empty station to Set
if !bestStation.isEmpty {
finalStations.insert(bestStation)
}
}
}
print(finalStations) // -> ["kone", "kfive", "ktwo", "kthree"]