Remove Both Extremes by Trimming Ends. Given distinct integers nums, remove both its smallest and largest values using deletions only from the front or back. Return the least number of deletions. In a one-entry array the same entry is both extremes and needs only one deletion.
### Problem
You are given an array `nums` of distinct integers. In one deletion, remove either the current first entry or the current last entry. Return the minimum number of deletions needed to remove both the smallest and largest values from the original array. If the array has one entry, that same entry is both extremes and one deletion removes it.
```cpp
class Solution {
public:
int minimumDeletions(vector<int>& nums);
};
```
### Examples
**Example 1:**
```text
nums = [4,1,3,7,2]
Output: 4
Explanation: Removing the first two and last two entries removes 1 and 7.
```
**Example 2:**
```text
nums = [9,2,5]
Output: 2
Explanation: Removing the first two entries removes both extremes.
```
### Constraints
- `1 <= nums.length <= 10^5`.
- Values are distinct integers in `[-10^5,10^5]`.
### Clarifying Questions
One deletion removes one current endpoint. Interior entries cannot be deleted directly.
```hint Inspect the structure
After ordering the two extreme positions, which three ways can end-trimming remove both?
```
### Reasoning checkpoints
- Use only deletions from the current ends, with every removed entry counted.
- Cover both extreme values and justify that fewer deletions cannot suffice.
- Handle extremes near either end, next to each other, or at the same singleton position.
### Optional extensions
How would multiple occurrences of either extreme change the removal goal?
Overview: Remove Both Extremes by Trimming Ends. Given distinct integers nums, remove both its smallest and largest values using deletions only from the front or back. Return the least number of deletions. In a one-entry array the same entry is both extremes and needs only one deletion.
You are given an array nums of distinct integers. In one deletion, remove either the current first entry or the current last entry. Return the minimum number of deletions needed to remove both the smallest and largest values from the original array. If the array has one entry, that same entry is both extremes and one deletion removes it.
class Solution {
public:
int minimumDeletions(vector<int>& nums);
};
Examples
Example 1:
nums = [4,1,3,7,2]
Output: 4
Explanation: Removing the first two and last two entries removes 1 and 7.
Example 2:
nums = [9,2,5]
Output: 2
Explanation: Removing the first two entries removes both extremes.
Constraints
1 <= nums.length <= 10^5
.
Values are distinct integers in
[-10^5,10^5]
.
Clarifying Questions Guidance
One deletion removes one current endpoint. Interior entries cannot be deleted directly.
Reasoning checkpoints
Use only deletions from the current ends, with every removed entry counted.
Cover both extreme values and justify that fewer deletions cannot suffice.
Handle extremes near either end, next to each other, or at the same singleton position.
Optional extensions
How would multiple occurrences of either extreme change the removal goal?