FastPrepSelect Least Resource Tasks
Problem

Select Least Resource Tasks

Learn this problem
Amazon logoAmazonNEW GRADINTERNOA
See Amazon hiring insights

Problem statement

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

Repeat the following process until no tasks remain:

  • Select the remaining task with the lowest resource consumption. If multiple tasks have 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.

Complete the function selectLeastResourceTasks, which receives resourceConsumption and returns the total as an int.

Function

selectLeastResourceTasks(resourceConsumption: int[]) → int

Examples

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.

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
checking account