LeetCode Entry
3723. Maximize Sum of Squares of Digits
String of max n digits summing up to s
3723. Maximize Sum of Squares of Digits medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1511
Problem TLDR
String of max n digits summing up to s
Intuition
Take all the nines. Then fit the leftovers.
Approach
- the actual number format is 999+{0..8}000+
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\)
Code
fun maxSumOfSquares(n: Int, s: Int) =
if (s>9*n) "" else ("9".repeat(s/9)+s%9).take(n).padEnd(n,'0')
pub fn max_sum_of_squares(n: i32, s: i32) -> String {
(0..n*(s<=9*n)as i32).map(|i|((s-9*i).clamp(0,9)+48)as u8 as char).collect()
}
Comments