[LeetCode] Minimum Element After Replacement With Digit Sum

3300. Minimum Element After Replacement With Digit Sum

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main

import "math"

func helper(x int) int {
res := 0
for x > 0 {
res += x % 10
x /= 10
}
return res
}

func minElement(nums []int) int {
res := math.MaxInt64
for _, n := range nums {
res = min(res, helper(n))
}
return res
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/09/29/PS/LeetCode/minimum-element-after-replacement-with-digit-sum/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.