Maximum Easy Tasks After Rearranging
Problem statement
You are given an integer array taskDifficulty. You may rearrange its elements in any order.
A task is easy when both conditions hold:
- It has an immediate neighbor on its left and on its right, so the first and last positions cannot be easy.
- Its difficulty is strictly less than the difficulties of both immediate neighbors.
Return the maximum possible number of easy tasks after rearranging the array.
Function
getMaximumTasks(taskDifficulty: int[]) → intExamples
Example 1
taskDifficulty = [4,5,6,1,2]return = 2One optimal order is [4,1,5,2,6]. The values 1 and 2 are each strictly smaller than both immediate neighbors, so two tasks are easy.
Constraints
1 <= taskDifficulty.length <= 2 * 10^5.1 <= taskDifficulty[i] <= 10^9.