Minimize Task Resource Consumption
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:
- Select the remaining task with the smallest resource consumption. If several tasks share that value, choose the one with the smallest current index.
- Add its value to the total.
- 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[]) → intExamples
Example 1
resourceConsumption = [4,3,2,1]return = 4Select 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 = 4Select 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 = 3The 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 <= 20001 <= resourceConsumption[i] <= 10^5