LeetCode Entry
2567. Minimum Score by Changing Two Elements
Min(maxdiff+mindiff) after changing any two numbers
2567. Minimum Score by Changing Two Elements medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1547
Problem TLDR
Min(maxdiff+mindiff) after changing any two numbers
Intuition
This is the corner cases brain teaser. Sort. Three cases: change first two, last two or first and last.
Approach
- Rust itertools: k_largest, k_smallest
- zip prefix and suffix of sorted order and take min diff
Complexity
-
Time complexity: \(O(nlogn | n)\)
-
Space complexity: \(O(1)\)
Code
fun minimizeSum(n: IntArray)=
n.sorted().run{takeLast(3).zip(take(3),Int::minus).min()}
pub fn minimize_sum(n: Vec<i32>) -> i32 {
n.iter().k_largest(3).rev().zip(n.iter().k_smallest(3)).map(|(a, b)| a - b).min().unwrap()
}
Comments