forked from egonSchiele/grokking_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_set_convering.hs
32 lines (26 loc) · 1.17 KB
/
01_set_convering.hs
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
import qualified Data.Set as Set
import qualified Data.HashMap.Strict as Map
stations = Map.fromList [
("kone", Set.fromList(["id", "nv", "ut"])),
("ktwo", Set.fromList(["wa", "id", "mt"])),
("kthree", Set.fromList(["or", "nv", "ca"])),
("kfour", Set.fromList(["nv", "ut"])),
("kfive", Set.fromList(["ca", "az"]))
]
statesNeeded = Set.fromList ["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]
bestStation statesNeeded selectedStations stations = foldl
(\a@(station1, states1) b@(station2, states2) ->
let fn states = Set.size $ (Set.intersection statesNeeded states)
coverage1 = fn states1
coverage2 = fn states2
in if coverage1 > coverage2 then a else b
)
x
xs
where (x: xs) = filter (\(station, states) -> not $ station `elem` selectedStations) $ Map.toList stations
stationSet statesNeeded finalStations =
let (station, coveredStations) = bestStation statesNeeded finalStations stations
neededStations = Set.difference statesNeeded coveredStations
newStations = station : finalStations
in if (Set.size statesNeeded > 0) then stationSet neededStations newStations else finalStations
finalSet = stationSet statesNeeded []