#3723

Maximize Sum of Squares of Digits

medium · 58.9% accepted · 62 likes · top 56%

math · greedy

Description

You are given two positive integers num and sum.

A positive integer n is good if it satisfies both of the following:

- The number of digits in n is exactly num.

- The sum of digits in n is exactly sum.

The score of a good integer n is the sum of the squares of digits in n.

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.

Solution