Removing Minimum and Maximum From Array (LeetCode 2091): Solution and Explanation
You may only delete the first or last element of an array of distinct integers. Return the fewest deletions that remove both the minimum and the maximum.
Intuition
Deletions only nibble at the two ends, so an element disappears exactly when the deletions from its side reach it. What matters is where the minimum and the maximum sit, not their values.
Call the left target's index i and the right target's index j. Some prefix of the array is deleted from the front and some suffix from the back, and together they must cover both i and j.
That leaves only three sensible plans. Delete from the front far enough to reach j, which also passes i. Delete from the back far enough to reach i, which also passes j. Or delete from the front through i and from the back through j.
Any other plan deletes more than one of these, so the answer is the smallest of the three counts.
Approach: Locate both targets and compare three plans
- Scan the array once and record
mnandmx, the indices of the minimum and maximum. - Let
i = min(mn, mx)andj = max(mn, mx). - Front only costs
j + 1. Back only costsn - i. - Both ends costs
(i + 1) + (n - j). - Return the minimum of the three.
Step-by-step example
nums = [2,10,7,5,4,1,8,6]
- The scan finds the maximum 10 at index 1 and the minimum 1 at index 5.
- So i = 1 and j = 5, with n = 8.
- Front only: delete indices 0 to 5, which is 6 deletions.
- Back only: delete indices 7 down to 1, which is 8 - 1 = 7 deletions.
- Both ends: 2 from the front and 3 from the back, 5 deletions. The answer is 5.
Code: Python, Java, C++
1class Solution:
2 def minimumDeletions(self, nums: List[int]) -> int:
3 n = len(nums)
4 mn = mx = 0
5 for k in range(n):
6 if nums[k] < nums[mn]:
7 mn = k
8 if nums[k] > nums[mx]:
9 mx = k
10 i, j = min(mn, mx), max(mn, mx)
11 front = j + 1
12 back = n - i
13 both = (i + 1) + (n - j)
14 return min(front, back, both)1class Solution {
2 public int minimumDeletions(int[] nums) {
3 int n = nums.length;
4 int mn = 0, mx = 0;
5 for (int k = 0; k < n; k++) {
6 if (nums[k] < nums[mn]) {
7 mn = k;
8 }
9 if (nums[k] > nums[mx]) {
10 mx = k;
11 }
12 }
13 int i = Math.min(mn, mx), j = Math.max(mn, mx);
14 int front = j + 1;
15 int back = n - i;
16 int both = (i + 1) + (n - j);
17 return Math.min(front, Math.min(back, both));
18 }
19}1class Solution {
2public:
3 int minimumDeletions(vector<int>& nums) {
4 int n = nums.size();
5 int mn = 0, mx = 0;
6 for (int k = 0; k < n; k++) {
7 if (nums[k] < nums[mn]) {
8 mn = k;
9 }
10 if (nums[k] > nums[mx]) {
11 mx = k;
12 }
13 }
14 int i = min(mn, mx), j = max(mn, mx);
15 int front = j + 1;
16 int back = n - i;
17 int both = (i + 1) + (n - j);
18 return min(front, min(back, both));
19 }
20};Time and space complexity
O(n)
One pass finds both indices, and the three plans take constant time.
O(1)
Only a handful of indices and counts are stored.
Other approaches
Try every front and back split (time O(n), space O(1))
For each number of front deletions, compute how many back deletions are still needed and keep the best total. It reaches the same answer but checks n + 1 splits instead of three.
Simulate with a deque (time O(n), space O(n))
Greedily pop from whichever end is closer to the next target. It is easy to get wrong, since the closer end for one target can be the wrong choice overall, and it does extra work.
Common mistakes
- Forgetting that the minimum and maximum can be in either order, so always sort the two indices into i and j first.
- Off-by-one errors: reaching index j from the front takes j + 1 deletions, and reaching index i from the back takes n - i.
- Only comparing front-only and back-only. Example 1 shows the both-ends plan can win.
- Not handling a single element, where the minimum and maximum are the same element and one deletion is enough.
Frequently asked questions
Why are only three plans needed?
Every deletion sequence removes a prefix and a suffix. To cover both targets, either the prefix reaches j, the suffix reaches i, or the prefix reaches i and the suffix reaches j. The cheapest version of each case is one of the three plans.
Does it matter which target is the minimum?
No. After the scan only the two positions matter, so the solution sorts them and forgets which was which.
What if the array has one element?
Then i = j = 0 and all three plans cost 1, which is correct.