LeetCode Entry
2091. Removing Minimum and Maximum From Array
Remove min and max by cutting the tails
2091. Removing Minimum and Maximum From Array medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1467
Problem TLDR
Remove min and max by cutting the tails
Intuition
Either remove suffix of both, or prefix of both, or each own tail suffix and prefix.
Approach
- shortest varian by using ‘sorted’
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
fun minimumDeletions(n: IntArray) = n.run {
val (a, b) = listOf(indexOf(min()), indexOf(max())).sorted()
minOf(b + 1, size - a, a + 1 + size - b)
}
pub fn minimum_deletions(n: Vec<i32>) -> i32 {
let (i, j) = (n.iter().position_min().unwrap(), n.iter().position_max().unwrap());
(i.max(j) + 1).min(n.len() - i.min(j)).min(n.len() + 1 - i.abs_diff(j)) as _
}
Comments