Visa Interview Question has me stomped for a while.

huangapple go评论64阅读模式
英文:

Visa Interview Question has me stomped for a while

问题

以下是您提供的文本的翻译部分:

问题

在一个地区有一些工厂生产有污染的气体,需要在每个工厂安装过滤器来减少污染。

每个安装的过滤器会将该工厂的污染减半。每个工厂可以安装多个过滤器。
有一个包含N个整数的列表,表示该地区每个工厂的污染水平。

找出需要减半总体污染的最小过滤器数量。

例如 - 让[3, 5, 6, 1, 18]是5个工厂的污染水平列表。
总体污染= 3 + 5 + 6 + 1 + 18 = 33(目标是33/2 = 16.5)

在索引为4的工厂安装一个过滤器 --> 污染水平将变为[3, 5, 6, 1, 9] --> 24
在索引为4的工厂安装一个过滤器 --> 污染水平将变为[3, 5, 6, 1, 4.5] --> 19.5
在索引为2的工厂安装一个过滤器 --> 污染水平将变为[3, 5, 3, 1, 4.5] --> 16.5

需要最少3个过滤器来将总体污染减半。

N是一个在范围[1....30,000]内的整数。列表中的每个元素是在范围[0....70,000]内的整数。

我并不是懒惰,这是我的错误尝试:

public static int getMinimumFilters(double[] pollutionLevels) {
    double totalPollution = Arrays.stream(pollutionLevels).sum();
    double targetPollution = totalPollution / 2.0;
    int numFilters = 0;
    double currentPollution = 0;
    double tempPollution = 0;

    Arrays.sort(pollutionLevels);

    for (double i = pollutionLevels.length - 1; i >= 0; i--) {
        currentPollution = (totalPollution - pollutionLevels[(int) i]) + pollutionLevels[(int) i] / 2;
        numFilters++;
        if (currentPollution <= targetPollution) {
            break;
        }
    }

    return numFilters;
}

public static void main(String[] args) {
    double[] pollutionLevels = {3, 5, 6, 1, 18};
    System.out.println("Minimum number of filters needed: " + getMinimumFilters(pollutionLevels));
}

我的下一个思路是将数组求和,然后使用总和与targetPollution值之间的差来检查应该减半哪些数组值以达到目标。

任何帮助都将不胜感激。

英文:

I have been trying to resolve this interview question but so far I have not been able to. Please help.

Question

There are factories in an area which produce a pollutive gas and filters are to be installed at each factory to reduce pollution.

Each filter installed would half the pollution in that factory. Each factory can have multiple filters.
There is a list of N integers representing the level of pollution in each of the N factories in the area.

Find the minimum number of filters needed to half the overall pollution.

E.g. - Let [3, 5, 6, 1, 18] be the list of pollution levels in 5 factories.
Overall pollution = 3+5+6+1+18 = 33 (target is 33/2 = 16.5)

Install a filter in factory given by index=4 -- > pollution levels will be [3, 5, 6, 1, 9] --> 24
Install a filter in factory given by index=4 -- > pollution levels will be [3, 5, 6, 1, 4.5] --> 19.5
Install a filter in factory given by index=2 -- > pollution levels will be [3, 5, 3, 1, 4.5] --> 16.5

Need 3 filters minimum to half the overall pollution.

N is an integer within the range [1....30,000]. Each element in the list is an integer within the range [0....70,000]

I am not being lazy, here's my incorrect attempt:

public static int getMinimumFilters(double[] pollutionLevels) {
    double totalPollution = Arrays.stream(pollutionLevels).sum();
    double targetPollution = totalPollution / 2.0;
    int numFilters = 0;
    double currentPollution = 0;
    double tempPollution = 0;

    Arrays.sort(pollutionLevels);

    for (double i = pollutionLevels.length -1; i &gt;= 0; i--) {
        currentPollution = (totalPollution - pollutionLevels[(int) i]) + pollutionLevels[(int) i]/2;
        numFilters++;
        if (currentPollution &lt;= targetPollution) {
            break;
        }

    }

    return numFilters;
}

public static void main(String[] args) {
    double[] pollutionLevels = {3, 5, 6, 1, 18};
    System.out.println(&quot;Minimum number of filters needed: &quot; + getMinimumFilters(pollutionLevels));
}

My next line of thought was to sum the array and then use the difference between the sum and the targetPollution value to somehow check which array values should be halved to achieve the target.

Any help is appreciated.

答案1

得分: 2

这是一个经典的“堆”问题,需要在插入和删除元素时保持元素有序,本例中我们从堆中移除最大的污染值,插入其一半的值,并从堆的顶部取出下一个最大值,我们可以在Java中使用PriorityQueue

public static int getMinimumFilters(double[] pollutionLevels) {
    double currentPollution = Arrays.stream(pollutionLevels).sum();
    double targetPollution = currentPollution / 2.0;
    PriorityQueue<Double> queue = new PriorityQueue<>(10, Collections.reverseOrder());
    for(double level : pollutionLevels) {
        queue.offer(level);
    }
    int numFilters = 0;
    while(currentPollution > targetPollution) {
        double maxPollution = queue.poll(); 
        double pollutionAfterFilter = maxPollution/2.0;
        queue.offer(pollutionAfterFilter); 
        currentPollution -= pollutionAfterFilter;
        numFilters++;
    }
    return numFilters;
}

public static void main(String[] args) {
    double[] pollutionLevels = {3, 5, 6, 1, 18};
    System.out.println("Minimum number of filters needed: " + getMinimumFilters(pollutionLevels));
}
英文:

This is a classic heap problem where you need to keep the elements sorted while insertion and deletion, in this case we remove max pollution from heap and insert half it's value and pull the next max value right from top of the heap, we could use PriorityQueue in java

public static int getMinimumFilters(double[] pollutionLevels) {
    double currentPollution = Arrays.stream(pollutionLevels).sum();
    double targetPollution = currentPollution / 2.0;
    PriorityQueue&lt;Double&gt; queue = new PriorityQueue&lt;&gt;(10, Collections.reverseOrder());
    for(double level : pollutionLevels) {
        queue.offer(level);
    }
    int numFilters = 0;
    while(currentPollution &gt; targetPollution) {
        double maxPollution = queue.poll(); 
        double pollutionAfterFilter = maxPollution/2.0;
        queue.offer(pollutionAfterFilter); 
        currentPollution -= pollutionAfterFilter;
        numFilters++;
    }
    return numFilters;
}

public static void main(String[] args) {
    double[] pollutionLevels = {3, 5, 6, 1, 18};
    System.out.println(&quot;Minimum number of filters needed: &quot; + getMinimumFilters(pollutionLevels));
}

huangapple
  • 本文由 发表于 2023年6月6日 01:14:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定