Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

number of filters

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])}`);
Comment

PREVIOUS NEXT
Code Example
Javascript :: Maxscript Bitarray 
Javascript :: js organise string tab spaced 
Javascript :: Ajax in wordpredss 
Javascript :: how to get value of selected radio button in javascript 
Javascript :: multply js 
Javascript :: javascript spread operator works on what structure 
Javascript :: counting number of times a string is in another string 
Javascript :: generate html by javascript 
Javascript :: rotate image javascript base64 
Javascript :: react three fiber set cursor pointer 
Javascript :: searchable 
Javascript :: how to add another model into type of model in mongodb schema 
Javascript :: nav hover add class and remove using javascript smooth 
Javascript :: create index with multiple fields mongo 
Javascript :: jquery unique 
Javascript :: javascript vuelidate identical passwords only if checkbox is ticked 
Javascript :: Why is the return function in my debounce function never called? Angularjs 
Javascript :: how to replace img url using jquery on mobile screen 
Javascript :: AngularJS disable default form submit hook 
Javascript :: Conditional navigation inside Tabs 
Javascript :: react-native installation error with npx react-native 
Javascript :: upsert typeorm 
Javascript :: Express.js View "globals" 
Javascript :: nextjs app wdyr 
Javascript :: Appium find Android Element with Xpath using Javascript 
Javascript :: javasrccipt loop array 
Javascript :: vimscript replace function 
Javascript :: jquery ajax success function not executing 
Javascript :: react console logs not working 
Javascript :: save to text or html file very good 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =