3723. Maximize Sum of Squares of Digits Medium

@problem@discussion
#Math#Greedy



1/**
2 * [3723] Maximize Sum of Squares of Digits
3 *
4 * You are given two positive integers num and sum.
5 * A positive integer n is good if it satisfies both of the following:
6 * 
7 * 	The number of digits in n is exactly num.
8 * 	The sum of digits in n is exactly sum.
9 * 
10 * The score of a good integer n is the sum of the squares of digits in n.
11 * Return a string denoting the good integer n that achieves the maximum score. If there are multiple possible integers, return the maximum ​​​​​​​one. If no such integer exists, return an empty string.
12 *  
13 * <strong class="example">Example 1:
14 * <div class="example-block">
15 * Input: <span class="example-io">num = 2, sum = 3</span>
16 * Output: <span class="example-io">"30"</span>
17 * Explanation:
18 * There are 3 good integers: 12, 21, and 30.
19 * 
20 * 	The score of 12 is 1^2 + 2^2 = 5.
21 * 	The score of 21 is 2^2 + 1^2 = 5.
22 * 	The score of 30 is 3^2 + 0^2 = 9.
23 * 
24 * The maximum score is 9, which is achieved by the good integer 30. Therefore, the answer is "30".
25 * </div>
26 * <strong class="example">Example 2:
27 * <div class="example-block">
28 * Input: <span class="example-io">num = 2, sum = 17</span>
29 * Output: <span class="example-io">"98"</span>
30 * Explanation:
31 * There are 2 good integers: 89 and 98.
32 * 
33 * 	The score of 89 is 8^2 + 9^2 = 145.
34 * 	The score of 98 is 9^2 + 8^2 = 145.
35 * 
36 * The maximum score is 145. The maximum good integer that achieves this score is 98. Therefore, the answer is "98".
37 * </div>
38 * <strong class="example">Example 3:
39 * <div class="example-block">
40 * Input: <span class="example-io">num = 1, sum = 10</span>
41 * Output: <span class="example-io">""</span>
42 * Explanation:
43 * There are no integers that have exactly 1 digit and whose digits sum to 10. Therefore, the answer is "".
44 * </div>
45 *  
46 * Constraints:
47 * 
48 * 	1 <= num <= 2 * 10^5
49 * 	1 <= sum <= 2 * 10^6
50 * 
51 */
52pub struct Solution {}
53
54// problem: https://leetcode.com/problems/maximize-sum-of-squares-of-digits/
55// discuss: https://leetcode.com/problems/maximize-sum-of-squares-of-digits/discuss/?currentPage=1&orderBy=most_votes&query=
56
57// submission codes start here
58
59impl Solution {
60    pub fn max_sum_of_squares(num: i32, sum: i32) -> String {
61        String::new()
62    }
63}
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_3723() {
73    }
74}
75

Back
© 2026 bowen.ge All Rights Reserved.