FastPrepMinimize Task Resource Consumption

Minimize Task Resource Consumption

NVIDIA logoNVIDIA● MediumINTERNNEW GRADOA

Problem statement

You are given an integer array resourceConsumption. Each value is the resource consumption of a task.

Repeat these steps until no tasks remain:

  1. Select the remaining task with the smallest resource consumption. If several tasks share that value, choose the one with the smallest current index.
  2. Add its value to the total.
  3. Remove the selected task and its immediate neighbors in the remaining array, if they exist.

Removal closes the gaps, so tasks that were separated can become neighbors. Return the total resource consumption of the selected tasks.

Function

minimizeTaskResourceConsumption(resourceConsumption: int[]) → int

Examples

Example 1

resourceConsumption = [4,3,2,1]return = 4

Select 1 and remove it with 2, leaving [4,3]. Then select 3 and remove it with 4. The total is 1 + 3 = 4.

Example 2

resourceConsumption = [4,9,1,8,3]return = 4

Select 1 and remove it with its neighbors 9 and 8, leaving [4,3]. These two tasks are now neighbors. Select 3 and remove both. The total is 1 + 3 = 4.

Example 3

resourceConsumption = [1,1,1,1,1]return = 3

The leftmost task wins each tie. The remaining arrays are [1,1,1], then [1], then []. Three selected values of 1 give a total of 3.

Constraints

  • 3 <= resourceConsumption.length <= 2000
  • 1 <= resourceConsumption[i] <= 10^5

More NVIDIA problems

See NVIDIA hiring insights
Saved
public int minimizeTaskResourceConsumption(int[] resourceConsumption) {
    // Write your code here.
}
resourceConsumption[4,3,2,1]
expected4
Checking account…