[LeetCode] 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.

1
2
3
4
5
6
7
8
class Solution {
public:
int minimumDeletions(vector<int>& A) {
int mi = min_element(begin(A), end(A)) - begin(A), ma = max_element(begin(A), end(A)) - begin(A);
int l = min(mi, ma), r = max(mi, ma), n = A.size();
return min({l + 1 + n - r, r + 1, n - l});
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/04/PS/LeetCode/removing-minimum-and-maximum-from-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.