reorg and add code for second edition
This commit is contained in:
28
10_greedy_algorithms/ES6/01_set_covering.js
Normal file
28
10_greedy_algorithms/ES6/01_set_covering.js
Normal file
@@ -0,0 +1,28 @@
|
||||
// You pass an array in, and it gets converted to a set.
|
||||
let statesNeeded = new Set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]);
|
||||
|
||||
const stations = {};
|
||||
stations.kone = new Set(["id", "nv", "ut"]);
|
||||
stations.ktwo = new Set(["wa", "id", "mt"]);
|
||||
stations.kthree = new Set(["or", "nv", "ca"]);
|
||||
stations.kfour = new Set(["nv", "ut"]);
|
||||
stations.kfive = new Set(["ca", "az"]);
|
||||
|
||||
const finalStations = new Set();
|
||||
|
||||
while (statesNeeded.size) {
|
||||
let bestStation = null;
|
||||
let statesCovered = new Set();
|
||||
Object.keys(stations).forEach(station => {
|
||||
const states = stations[station];
|
||||
const covered = new Set([...statesNeeded].filter(x => states.has(x)));
|
||||
if (covered.size > statesCovered.size) {
|
||||
bestStation = station;
|
||||
statesCovered = covered;
|
||||
}
|
||||
});
|
||||
statesNeeded = new Set([...statesNeeded].filter(x => !statesCovered.has(x)));
|
||||
finalStations.add(bestStation);
|
||||
}
|
||||
|
||||
console.log(finalStations); // Set { 'kone', 'ktwo', 'kthree', 'kfive' }
|
||||
Reference in New Issue
Block a user