Problem overview
The method biggestDigitSum receives a list of integers nums, each between 0 and 1,000,000,000, and returns the number whose digits add up to the most. The digit sum of 472 is 4 + 7 + 2 = 13. If two numbers have the same digit sum, the one that comes first wins. The method returns the number itself, not its digit sum.
For [472, 95, 1000] the digit sums are 13, 14 and 1, so the answer is 95. For [19, 55, 28] all three sums are 10, so the first, 19, wins. For [0, 7] the sums are 0 and 7, so the answer is 7.
Approach: a helper that sums digits
The work is split in two. The helper digitSum turns one number into its digit sum. biggestDigitSum then runs a "best so far" search over the list, comparing numbers by their digit sums instead of their values.
To read the digits of a number you do not need to turn it into text. The remainder of dividing by 10 is the last digit, and dividing by 10 and dropping the fraction removes it:
| n at the start of the turn | n % 10 (added) | n // 10 (next n) |
|---|---|---|
| 472 | 2 | 47 |
| 47 | 7 | 4 |
| 4 | 4 | 0 |
Adding up 2, 7 and 4 gives 13. Once the last digit is removed, n is 0 and the loop stops. The same loop gives 0 for 0 and 1 for 1000000000, because zeros add nothing.
Step by step
- In
digitSum, starttotal = 0. - Loop with
while n > 0. - Add
n % 10tototal. - Set
n = n // 10to drop the last digit. Without this linennever changes and the loop never ends. - After the loop, return
total. - In
biggestDigitSum, setbest = nums[0]. - Loop with
for num in nums, and ifself.digitSum(num) > self.digitSum(best), setbest = num. - After the loop, return
best.
The solution in Python
class Solution:
def digitSum(self, n: int) -> int:
total = 0
while n > 0:
total += n % 10
n = n // 10
return total
def biggestDigitSum(self, nums: List[int]) -> int:
best = nums[0]
for num in nums:
if self.digitSum(num) > self.digitSum(best):
best = num
return best
Details:
- Use
//, not/. In Python472 / 10is47.2, a float, while472 // 10is47. - For
n = 0the loop never runs and the helper returns 0, which is the right digit sum. - The loop starts with the first number, which is also
best. Comparing a number with itself changes nothing. - The comparison uses
>, not>=. On a tie,bestkeeps the number that came first, as the task asks. - The parameter
nis rebound inside the helper. The caller's number is not changed, because integers are immutable.
Why compare digit sums with a helper
The search is the familiar "best so far" pattern: remember a winner, let every number challenge it, and replace the winner when a challenger is better. The only new part is what "better" means, and that is hidden inside the helper. The loop body stays one line long, and the digit logic lives in one place. The helper is called twice per number, once for the challenger and once for the current best. That repeats some work, but it keeps the code short, and the numbers are at most ten digits long.
Testing
| nums | digit sums | result |
|---|---|---|
[472, 95, 1000] | 13, 14, 1 | 95 |
[19, 55, 28] | 10, 10, 10 | 19 |
[0, 7] | 0, 7 | 7 |
Common mistakes
- Returning the digit sum. The task wants the number: 95, not 14.
- Using
>=. Ties would go to the last number, not the first. - Forgetting
n = n // 10. The loop repeats forever. - Comparing the numbers themselves. The biggest number is not the one with the biggest digit sum: 1000 is the biggest value in the first example but has the smallest sum.
- Calling the helper without
self.. Inside a method of the class,digitSum(num)alone is aNameError.
