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.
Complete the function selectLeastResourceTasks.
selectLeastResourceTasks has the following parameter:
int resourceConsumption[n]: the resource-consumption values of the tasks
Returns
int: the total resource consumption of all selected tasks.
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.
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.
3 <= n <= 20001 <= resourceConsumption[i] <= 10^5
- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Product Category Group SizesPHONE SCREEN · Seen May 2026
- Count Connected ComponentsPHONE SCREEN · Seen May 2026
- Drone Delivery RouteOA · Seen May 2026
public int selectLeastResourceTasks(int[] resourceConsumption) {
// write your code here
}