38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
// Найти непокрытые штаты
|
|
// Выбрать странцию покрывающую больше всех штатов из непокрытых
|
|
|
|
const findStations = (allStationLst, needStateList) => {
|
|
let needed = new Set([...needStateList]);
|
|
let result = new Set();
|
|
|
|
while (needed.size > 0) {
|
|
let statesCovered = new Set();
|
|
let stationCandidate;
|
|
|
|
Object.keys(allStationLst).forEach((station) => {
|
|
const stationStateCovered = new Set([...allStationLst[station]].filter(item => needed.has(item)));
|
|
if (stationStateCovered.size > statesCovered.size) {
|
|
statesCovered = stationStateCovered;
|
|
stationCandidate = station;
|
|
}
|
|
})
|
|
|
|
result.add(stationCandidate);
|
|
needed = new Set([...needed].filter(item => !statesCovered.has(item)));
|
|
}
|
|
|
|
return result
|
|
};
|
|
|
|
const needStateList = new Set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]);
|
|
const stations = {
|
|
kone: new Set(["id", "nv", "ut"]),
|
|
ktwo: new Set(["wa", "id", "mt"]),
|
|
kthree: new Set(["or", "nv", "ca"]),
|
|
kfour: new Set(["nv", "ut"]),
|
|
kfive: new Set(["ca", "az"]),
|
|
};
|
|
|
|
|
|
console.log(findStations(stations, needStateList));
|