/**
* Problem: LeetCode 2091 - Removing Minimum and Maximum From Array
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int minimumDeletions(int[] nums) {
int n = nums.length;
// Find indices of min and max
int minIdx = 0, maxIdx = 0;
for (int i = 1; i < n; i++) {
if (nums[i] < nums[minIdx]) minIdx = i;
if (nums[i] > nums[maxIdx]) maxIdx = i;
}
// Make sure left <= right for convenience
int left = Math.min(minIdx, maxIdx);
int right = Math.max(minIdx, maxIdx);
// Strategy 1: Both from front → remove up to right+1 elements
int bothFront = right + 1;
// Strategy 2: Both from back → remove from left to end
int bothBack = n - left;
// Strategy 3: left from front, right from back
int splitWay = (left + 1) + (n - right);
return Math.min(bothFront, Math.min(bothBack, splitWay));
}
}
"""
Problem: LeetCode 2091 - Removing Minimum and Maximum From Array
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
n = len(nums)
if n <= 2:
return n
# Find indices of min and max
min_idx = 0
max_idx = 0
for i in range(1, n):
if nums[i] < nums[min_idx]:
min_idx = i
if nums[i] > nums[max_idx]:
max_idx = i
# Make sure left <= right for convenience
left = min(min_idx, max_idx)
right = max(min_idx, max_idx)
# Strategy 1: Both from front -> remove up to right + 1 elements
both_front = right + 1
# Strategy 2: Both from back -> remove from left to end
both_back = n - left
# Strategy 3: left from front, right from back
split_way = (left + 1) + (n - right)
return min(both_front, both_back, split_way)
/**
* Problem: LeetCode 2091 - Removing Minimum and Maximum From Array
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
using namespace std;
/**
* Problem: LeetCode 2091 - Removing Minimum and Maximum From Array
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int minimumDeletions(vector<int>& nums) {
int n = nums.size();
if (n <= 2) return n;
// Find indices of min and max
int minIdx = 0, maxIdx = 0;
for (int i = 1; i < n; i++) {
if (nums[i] < nums[minIdx]) minIdx = i;
if (nums[i] > nums[maxIdx]) maxIdx = i;
}
// Make sure left <= right for convenience
int left = min(minIdx, maxIdx);
int right = max(minIdx, maxIdx);
// Strategy 1: Both from front -> remove up to right + 1 elements
int bothFront = right + 1;
// Strategy 2: Both from back -> remove from left to end
int bothBack = n - left;
// Strategy 3: left from front, right from back
int splitWay = (left + 1) + (n - right);
return min({bothFront, bothBack, splitWay});
}
};
/**
* Problem: LeetCode 2091 - Removing Minimum and Maximum From Array
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var minimumDeletions = function(nums) {
const n = nums.length;
if (n <= 2) return n;
// Find indices of min and max
let minIdx = 0;
let maxIdx = 0;
for (let i = 1; i < n; i++) {
if (nums[i] < nums[minIdx]) minIdx = i;
if (nums[i] > nums[maxIdx]) maxIdx = i;
}
// Make sure left <= right for convenience
const left = Math.min(minIdx, maxIdx);
const right = Math.max(minIdx, maxIdx);
// Strategy 1: Both from front -> remove up to right + 1 elements
const bothFront = right + 1;
// Strategy 2: Both from back -> remove from left to end
const bothBack = n - left;
// Strategy 3: left from front, right from back
const splitWay = (left + 1) + (n - right);
return Math.min(bothFront, bothBack, splitWay);
};