3606 Minimum Element After Replacement With Digit Sum
3606 Minimum Element After Replacement With Digit Sum
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
2
1 <= nums.length <= 100
1 <= nums[i] <= 104
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
use std::cmp::min;
impl Solution {
pub fn min_element(nums: Vec<i32>) -> i32 {
let mut res = Vec::new();
for k in nums {
let mut a = 0;
let mut d = k;
while(d > 0) {
a += d % 10;
d = d / 10;
}
res.push(a);
}
let mut ans = 1000000;
for m in res {
if ans > m {
ans = m;
}
}
ans
}
}
This post is licensed under CC BY 4.0 by the author.