LeetCode Entry
3536. Maximum Product of Two Digits
Product of two largest digits in n
3536. Maximum Product of Two Digits easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1431
Problem TLDR
Product of two largest digits in n
Intuition
Convert to string. Sort.
Approach
- Rust itertools has k_largest
Complexity
-
Time complexity: \(O(logn*log log n)\)
-
Space complexity: \(O(logn)\)
Code
fun maxProduct(n: Int) =
"$n".map{it-'0'}.sorted().takeLast(2).let{it[0]*it[1]}
pub fn max_product(n: i32) -> i32 {
format!("0{n}").bytes().k_largest(2).map(|b| b as i32 - 48).product()
}
Comments