May 29Easy
Minimum element after replacement with digit sum
- Array
- Math
Problem
You are given an integer array nums. You replace each element in nums with the sum of its digits. Return the minimum element in nums after all replacements.
Solution
class Solution:
def minElement(self, nums: List[int]) -> int:
min_sum = float("inf")
for num in nums:
curr_sum = 0
while num:
curr_sum += num % 10
num //= 10
min_sum = min(min_sum, curr_sum)
return min_sum