liczba filtrów
function solution(N) {
const totalPollution = N.reduce((a, b) => a + b, 0);
const expectedPollution = totalPollution / 2;
console.log(`Total pollution: ${totalPollution}`);
console.log(`Expected pollution: ${expectedPollution}`);
let filterRequired = 0;
const arr = [...N].sort((a, b) => a - b).reverse();
let factories = [];
for (let i = 0; i < arr.length; i++) {
factories.push({
name: `factory ${i + 1}`,
filterApplied: 0,
pollution: arr[i],
originalPollution: arr[i],
index: i,
});
}
const applyFilter = factories => {
if (factories[0].pollution > 0) {
factories[0].filterApplied++;
factories[0].pollution = factories[0].pollution / 2;
arr[factories[0].index] = factories[0].pollution;
filterRequired++;
console.log(`Filter applied to ${factories[0].name} and factory filter number ${factories[0].filterApplied}`);
console.log(`Total pollution after filter ${filterRequired}: ${arr.reduce((a, b) => a + b, 0)}`);
}
}
const isPollutionReduced = () => {
const totalPollution = arr.reduce((a, b) => a + b, 0);
return totalPollution <= expectedPollution;
}
applyFilter(factories);
while (!isPollutionReduced()) {
factories.sort((a, b) => a.pollution - b.pollution).reverse();
applyFilter(factories);
}
console.log(factories);
return filterRequired;
}
console.log(`Total filter needed: ${solution([5, 19, 8, 1])}`);
// console.log(`Total filter needed: ${solution([3,0,5])}`);
Shubhangi_Mishra