LeetCode Entry
3010. Divide an Array Into Subarrays With Minimum Cost I
Split on 2 min values
3010. Divide an Array Into Subarrays With Minimum Cost I easy blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1255
Problem TLDR
Split on 2 min values #easy #quickselect
Intuition
Array is 50 elements. Sort and take 2. Or scan and deal with ifs.
Approach
- bitmask solution is also possible, enabling a branchless vectorizable code
Complexity
-
Time complexity: \(O(nlog(n))\), fastest is O(n)
-
Space complexity: \(O(n)\), fastest is O(1)
Code
// 10ms
fun minimumCost(n: IntArray) =
n.run{sort(1);n[0]+n[1]+n[2]}
/*
n.run{sort(1);n.take(3).sum()}
n[0]+n.drop(1).sorted().take(2).sum()
*/
// 0ms
pub fn minimum_cost(mut n: Vec<i32>) -> i32 {
n[1..].sort();n[0]+n[1]+n[2]
}
/*
n[1..].select_nth_unstable(1);n[0]+n[1]+n[2]
n[0] + n[1..].iter().sorted().take(2).sum::<i32>()
let (a,b)=n[1..].iter().fold((0u64,0),|(a,b),&x|(a|(1<<x),b|a&(1<<x)));
n[0] + (a.trailing_zeros() + (b | a & a - 1).trailing_zeros()) as i32
*/