-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3300-MinimumElementAfterReplacementWithDigitSum.go
More file actions
69 lines (59 loc) · 1.73 KB
/
3300-MinimumElementAfterReplacementWithDigitSum.go
File metadata and controls
69 lines (59 loc) · 1.73 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
// 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.
// Example 1:
// Input: nums = [10,12,13,14]
// Output: 1
// Explanation:
// nums becomes [1, 3, 4, 5] after all replacements, with minimum element 1.
// Example 2:
// Input: nums = [1,2,3,4]
// Output: 1
// Explanation:
// nums becomes [1, 2, 3, 4] after all replacements, with minimum element 1.
// Example 3:
// Input: nums = [999,19,199]
// Output: 10
// Explanation:
// nums becomes [27, 10, 19] after all replacements, with minimum element 10.
// Constraints:
// 1 <= nums.length <= 100
// 1 <= nums[i] <= 10^4
import "fmt"
func minElement(nums []int) int {
res := 1 << 31
calc := func(n int) int { // 123 => 1 + 2 + 3
res := 0
for n > 0 {
res += n % 10
n /= 10
}
return res
}
for _, v := range nums {
res = min(res, calc(v))
}
return res
}
func main() {
// Example 1:
// Input: nums = [10,12,13,14]
// Output: 1
// Explanation:
// nums becomes [1, 3, 4, 5] after all replacements, with minimum element 1.
fmt.Println(minElement([]int{10,12,13,14})) // 1
// Example 2:
// Input: nums = [1,2,3,4]
// Output: 1
// Explanation:
// nums becomes [1, 2, 3, 4] after all replacements, with minimum element 1.
fmt.Println(minElement([]int{1,2,3,4})) // 1
// Example 3:
// Input: nums = [999,19,199]
// Output: 10
// Explanation:
// nums becomes [27, 10, 19] after all replacements, with minimum element 10.
fmt.Println(minElement([]int{999,19,199})) // 10
}