Minimum Element After Replacement With Digit Sum (LeetCode 3300): Solution and Explanation
Replace every element of nums with the sum of its digits and return the smallest value that results.
Intuition
The question only asks for the minimum after the replacement, so the new array never needs to exist. Each digit sum can be compared the moment it is computed.
The digit sum of x comes from the classic peel loop: x % 10 is the last digit and x // 10 drops it. Repeat until x is 0.
A larger number can have a smaller digit sum (10000 sums to 1 while 99 sums to 18), so you cannot shortcut by picking the smallest original value.
Approach: Digit sum with a running minimum
- Set
bestto infinity. - For each
xinnums, sets = 0. - While
x > 0, addx % 10tosand setx //= 10. - Update
best = min(best, s). - Return
best.
Step-by-step example
nums = [10,12,13,14]
- best = infinity.
- 10 has digits 1 and 0, so its sum is 1. best becomes 1.
- 12 sums to 3, which is not below 1, so best stays 1.
- 13 sums to 4 and 14 sums to 5. Neither beats 1.
- Return 1.
Code: Python, Java, C++
1class Solution:
2 def minElement(self, nums: List[int]) -> int:
3 best = float('inf')
4 for x in nums:
5 s = 0
6 while x > 0:
7 s += x % 10
8 x //= 10
9 best = min(best, s)
10 return best1class Solution {
2 public int minElement(int[] nums) {
3 int best = Integer.MAX_VALUE;
4 for (int x : nums) {
5 int s = 0;
6 while (x > 0) {
7 s += x % 10;
8 x /= 10;
9 }
10 best = Math.min(best, s);
11 }
12 return best;
13 }
14}1class Solution {
2public:
3 int minElement(vector<int>& nums) {
4 int best = INT_MAX;
5 for (int x : nums) {
6 int s = 0;
7 while (x > 0) {
8 s += x % 10;
9 x /= 10;
10 }
11 best = min(best, s);
12 }
13 return best;
14 }
15};Time and space complexity
O(n log M)
Each of the n numbers is peeled once, and a number up to M has about log10(M) digits, at most 5 here.
O(1)
Only best, s and the shrinking copy of x are stored.
Other approaches
Build the replaced array (time O(n log M), space O(n))
Write each digit sum back into nums (or a new list) and call min at the end. It mirrors the statement exactly but uses an extra pass or extra memory.
String conversion (time O(n log M), space O(log M))
Sum int(c) over str(x) for each element. Short to write, but it creates a string per element.
Common mistakes
- Returning the smallest original number instead of the smallest digit sum. 10000 has a digit sum of 1 even though it is the largest value allowed.
- Starting
bestat 0, which would always be returned. Start at infinity or at the first digit sum. - Using
/in JavaScript withoutMath.floor, which keeps fractions and never letsxreach 0 cleanly.
Frequently asked questions
Do I have to modify nums?
No. Only the minimum is returned, so computing each digit sum and comparing it right away is enough.
What is the largest possible digit sum here?
9999 gives 36, the largest digit sum for values up to 10^4. The smallest is 1, reached by 1, 10, 100, 1000 and 10000.
Can the answer ever be 0?
No. Every element is at least 1, so every digit sum is at least 1.