LeetCode Entry
3300. Minimum Element After Replacement With Digit Sum
Min of digits sum
3300. Minimum Element After Replacement With Digit Sum easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1374
Problem TLDR
Min of digits sum
Intuition
Compute the digits sum in a while loop, track the minimum.
Approach
- you can unroll the while loop
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
fun minElement(n: IntArray) =
n.minOf{"$it".sumOf{it-'0'}}
pub fn min_element(n: Vec<i32>) -> i32 {
n.iter().map(|x|x%10+x/10%10+x/100%10+x/1000%10+x/10000%10).min().unwrap()
}
Comments