Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed array of distinct integers nums.
There is an element in nums that has the lowest value and an element that has the highest value. We call them the minimum and maximum respectively. Your goal is to remove both these elements from the array.
A deletion is defined as either removing an element from the front of the array or removing an element from the back of the array.
Return the minimum number of deletions it would take to remove both the minimum and maximum element from the array.
Example 1:
Input: nums = [2,10,7,5,4,1,8,6] Output: 5 Explanation: The minimum element in the array is nums[5], which is 1. The maximum element in the array is nums[1], which is 10. We can remove both the minimum and maximum by removing 2 elements from the front and 3 elements from the back. This results in 2 + 3 = 5 deletions, which is the minimum number possible.
Example 2:
Input: nums = [0,-4,19,1,8,-2,-3,5] Output: 3 Explanation: The minimum element in the array is nums[1], which is -4. The maximum element in the array is nums[2], which is 19. We can remove both the minimum and maximum by removing 3 elements from the front. This results in only 3 deletions, which is the minimum number possible.
Example 3:
Input: nums = [101] Output: 1 Explanation: There is only one element in the array, which makes it both the minimum and maximum element. We can remove it with 1 deletion.
Constraints:
1 <= nums.length <= 105-105 <= nums[i] <= 105nums are distinct.Problem summary: You are given a 0-indexed array of distinct integers nums. There is an element in nums that has the lowest value and an element that has the highest value. We call them the minimum and maximum respectively. Your goal is to remove both these elements from the array. A deletion is defined as either removing an element from the front of the array or removing an element from the back of the array. Return the minimum number of deletions it would take to remove both the minimum and maximum element from the array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[2,10,7,5,4,1,8,6]
[0,-4,19,1,8,-2,-3,5]
[101]
maximum-points-you-can-obtain-from-cards)minimum-deletions-to-make-character-frequencies-unique)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2091: Removing Minimum and Maximum From Array
class Solution {
public int minimumDeletions(int[] nums) {
int mi = 0, mx = 0, n = nums.length;
for (int i = 0; i < n; ++i) {
if (nums[i] < nums[mi]) {
mi = i;
}
if (nums[i] > nums[mx]) {
mx = i;
}
}
if (mi > mx) {
int t = mx;
mx = mi;
mi = t;
}
return Math.min(Math.min(mx + 1, n - mi), mi + 1 + n - mx);
}
}
// Accepted solution for LeetCode #2091: Removing Minimum and Maximum From Array
func minimumDeletions(nums []int) int {
mi, mx, n := 0, 0, len(nums)
for i, num := range nums {
if num < nums[mi] {
mi = i
}
if num > nums[mx] {
mx = i
}
}
if mi > mx {
mi, mx = mx, mi
}
return min(min(mx+1, n-mi), mi+1+n-mx)
}
# Accepted solution for LeetCode #2091: Removing Minimum and Maximum From Array
class Solution:
def minimumDeletions(self, nums: List[int]) -> int:
mi = mx = 0
for i, num in enumerate(nums):
if num < nums[mi]:
mi = i
if num > nums[mx]:
mx = i
if mi > mx:
mi, mx = mx, mi
return min(mx + 1, len(nums) - mi, mi + 1 + len(nums) - mx)
// Accepted solution for LeetCode #2091: Removing Minimum and Maximum From Array
/**
* [2091] Removing Minimum and Maximum From Array
*
* You are given a 0-indexed array of distinct integers nums.
* There is an element in nums that has the lowest value and an element that has the highest value. We call them the minimum and maximum respectively. Your goal is to remove both these elements from the array.
* A deletion is defined as either removing an element from the front of the array or removing an element from the back of the array.
* Return the minimum number of deletions it would take to remove both the minimum and maximum element from the array.
*
* Example 1:
*
* Input: nums = [2,<u>10</u>,7,5,4,<u>1</u>,8,6]
* Output: 5
* Explanation:
* The minimum element in the array is nums[5], which is 1.
* The maximum element in the array is nums[1], which is 10.
* We can remove both the minimum and maximum by removing 2 elements from the front and 3 elements from the back.
* This results in 2 + 3 = 5 deletions, which is the minimum number possible.
*
* Example 2:
*
* Input: nums = [0,<u>-4</u>,<u>19</u>,1,8,-2,-3,5]
* Output: 3
* Explanation:
* The minimum element in the array is nums[1], which is -4.
* The maximum element in the array is nums[2], which is 19.
* We can remove both the minimum and maximum by removing 3 elements from the front.
* This results in only 3 deletions, which is the minimum number possible.
*
* Example 3:
*
* Input: nums = [<u>101</u>]
* Output: 1
* Explanation:
* There is only one element in the array, which makes it both the minimum and maximum element.
* We can remove it with 1 deletion.
*
*
* Constraints:
*
* 1 <= nums.length <= 10^5
* -10^5 <= nums[i] <= 10^5
* The integers in nums are distinct.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/removing-minimum-and-maximum-from-array/
// discuss: https://leetcode.com/problems/removing-minimum-and-maximum-from-array/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn minimum_deletions(nums: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2091_example_1() {
let nums = vec![2, 10, 7, 5, 4, 1, 8, 6];
let result = 5;
assert_eq!(Solution::minimum_deletions(nums), result);
}
#[test]
#[ignore]
fn test_2091_example_2() {
let nums = vec![0, -4, 19, 1, 8, -2, -3, 5];
let result = 3;
assert_eq!(Solution::minimum_deletions(nums), result);
}
#[test]
#[ignore]
fn test_2091_example_3() {
let nums = vec![101];
let result = 1;
assert_eq!(Solution::minimum_deletions(nums), result);
}
}
// Accepted solution for LeetCode #2091: Removing Minimum and Maximum From Array
function minimumDeletions(nums: number[]): number {
const n = nums.length;
if (n == 1) return 1;
let i = nums.indexOf(Math.min(...nums));
let j = nums.indexOf(Math.max(...nums));
let left = Math.min(i, j);
let right = Math.max(i, j);
// 左右 left + 1 + n - right
// 两个都是左边 left + 1 + right - left = right + 1
// 都是右边 n - right + right - left = n - left
return Math.min(left + 1 + n - right, right + 1, n - left);
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.