Practice/Amazon/Leetcode 2091. Removing Minimum and Maximum From Array
CodingOptional
You are given an array of distinct integers. In a single operation, you can delete an element from either the beginning or the end of the array. Your task is to determine the minimum number of deletion operations needed to remove both the minimum and maximum elements from the array.
Note that once an element is deleted from an end, the array shrinks and new elements become accessible from that end.
Example 1:
Input: nums = [2, 10, 7, 5, 4, 1, 8, 6] Output: 5 Explanation: The minimum element is 1 at index 5, and the maximum element is 10 at index 1. We can delete from the left to index 1 (2 deletions) and from the right to index 5 (3 deletions). Total: 2 + 3 = 5 deletions.
Example 2:
Input: nums = [0, -4, 19, 1, 4, -2] Output: 3 Explanation: The minimum element is -4 at index 1, and the maximum element is 19 at index 2. We can delete from the left up to and including index 2, which removes both elements. Total: 3 deletions.
Example 3:
Input: nums = [101] Output: 1 Explanation: The single element is both the minimum and maximum, so we need 1 deletion.