Problem

Select Least Resource Tasks

NEW GRADOA
See Amazon online assessment and hiring insights

Amazon's Elastic Container Service schedules tasks dynamically. Each task has a resource-consumption value. You are given an array resourceConsumption, where resourceConsumption[i] is the resource consumption of one task.

To clear tasks, repeat the following process until no tasks remain:

  • Select the remaining task with the lowest resource consumption. If more than one remaining task has the same lowest value, select the one with the smallest current index.
  • Add the selected task's resource consumption to the total.
  • Remove the selected task and its adjacent remaining tasks, if they exist.

Return the total resource consumption of all selected tasks.

Function Description

Complete the function selectLeastResourceTasks.

selectLeastResourceTasks has the following parameter:

  1. int resourceConsumption[n]: the resource-consumption values of the tasks

Returns

int: the total resource consumption of all selected tasks.

Examples
01 · Example 1
resourceConsumption = [4, 3, 2, 1]
return = 4

The lowest value is 1, so it is selected and removed with its left neighbor 2. The remaining tasks are [4, 3]. Next, 3 is selected and removed with 4. The total is 1 + 3 = 4.

02 · Example 2
resourceConsumption = [6, 4, 9, 10, 34, 56, 54]
return = 68

First select 4 and remove it with adjacent values 6 and 9. The remaining tasks are [10, 34, 56, 54]. Next select 10 and remove 10 and 34. Finally select 54 and remove 56 and 54. The total selected consumption is 4 + 10 + 54 = 68.

Constraints
  • 3 <= n <= 2000
  • 1 <= resourceConsumption[i] <= 10^5
More Amazon problems
drafts saved locally
public int selectLeastResourceTasks(int[] resourceConsumption) {
  // write your code here
}
resourceConsumption[4, 3, 2, 1]
expected4
sign in to submit