|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 简单 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3300.Minimum%20Element%20After%20Replacement%20With%20Digit%20Sum/README.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3300. 替换为数位和以后的最小元素](https://leetcode.cn/problems/minimum-element-after-replacement-with-digit-sum) |
| 10 | + |
| 11 | +[English Version](/solution/3300-3399/3300.Minimum%20Element%20After%20Replacement%20With%20Digit%20Sum/README_EN.md) |
| 12 | + |
| 13 | +## 题目描述 |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>给你一个整数数组 <code>nums</code> 。</p> |
| 18 | + |
| 19 | +<p>请你将 <code>nums</code> 中每一个元素都替换为它的各个数位之 <strong>和</strong> 。</p> |
| 20 | + |
| 21 | +<p>请你返回替换所有元素以后 <code>nums</code> 中的 <strong>最小</strong> 元素。</p> |
| 22 | + |
| 23 | +<p> </p> |
| 24 | + |
| 25 | +<p><strong class="example">示例 1:</strong></p> |
| 26 | + |
| 27 | +<div class="example-block"> |
| 28 | +<p><span class="example-io"><b>输入:</b>nums = [10,12,13,14]</span></p> |
| 29 | + |
| 30 | +<p><span class="example-io"><b>输出:</b>1</span></p> |
| 31 | + |
| 32 | +<p><strong>解释:</strong></p> |
| 33 | + |
| 34 | +<p><code>nums</code> 替换后变为 <code>[1, 3, 4, 5]</code> ,最小元素为 1 。</p> |
| 35 | +</div> |
| 36 | + |
| 37 | +<p><strong class="example">示例 2:</strong></p> |
| 38 | + |
| 39 | +<div class="example-block"> |
| 40 | +<p><span class="example-io"><b>输入:</b>nums = [1,2,3,4]</span></p> |
| 41 | + |
| 42 | +<p><span class="example-io"><b>输出:</b>1</span></p> |
| 43 | + |
| 44 | +<p><b>解释:</b></p> |
| 45 | + |
| 46 | +<p><code>nums</code> 替换后变为 <code>[1, 2, 3, 4]</code> ,最小元素为 1 。</p> |
| 47 | +</div> |
| 48 | + |
| 49 | +<p><strong class="example">示例 3:</strong></p> |
| 50 | + |
| 51 | +<div class="example-block"> |
| 52 | +<p><span class="example-io"><b>输入:</b>nums = [999,19,199]</span></p> |
| 53 | + |
| 54 | +<p><span class="example-io"><b>输出:</b>10</span></p> |
| 55 | + |
| 56 | +<p><strong>解释:</strong></p> |
| 57 | + |
| 58 | +<p><code>nums</code> 替换后变为 <code>[27, 10, 19]</code> ,最小元素为 10 。</p> |
| 59 | +</div> |
| 60 | + |
| 61 | +<p> </p> |
| 62 | + |
| 63 | +<p><strong>提示:</strong></p> |
| 64 | + |
| 65 | +<ul> |
| 66 | + <li><code>1 <= nums.length <= 100</code></li> |
| 67 | + <li><code>1 <= nums[i] <= 10<sup>4</sup></code></li> |
| 68 | +</ul> |
| 69 | + |
| 70 | +<!-- description:end --> |
| 71 | + |
| 72 | +## 解法 |
| 73 | + |
| 74 | +<!-- solution:start --> |
| 75 | + |
| 76 | +### 方法一:模拟 |
| 77 | + |
| 78 | +我们可以遍历数组 $\textit{nums}$,对于每个数 $x$,我们计算其各个数位之和 $y$,取所有 $y$ 中的最小值即为答案。 |
| 79 | + |
| 80 | +时间复杂度 $O(n \times \log M)$,其中 $n$ 和 $M$ 分别是数组 $\textit{nums}$ 的长度和数组中的最大值。空间复杂度 $O(1)$。 |
| 81 | + |
| 82 | +<!-- tabs:start --> |
| 83 | + |
| 84 | +#### Python3 |
| 85 | + |
| 86 | +```python |
| 87 | +class Solution: |
| 88 | + def minElement(self, nums: List[int]) -> int: |
| 89 | + return min(sum(int(b) for b in str(x)) for x in nums) |
| 90 | +``` |
| 91 | + |
| 92 | +#### Java |
| 93 | + |
| 94 | +```java |
| 95 | +class Solution { |
| 96 | + public int minElement(int[] nums) { |
| 97 | + int ans = 100; |
| 98 | + for (int x : nums) { |
| 99 | + int y = 0; |
| 100 | + for (; x > 0; x /= 10) { |
| 101 | + y += x % 10; |
| 102 | + } |
| 103 | + ans = Math.min(ans, y); |
| 104 | + } |
| 105 | + return ans; |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +#### C++ |
| 111 | + |
| 112 | +```cpp |
| 113 | +class Solution { |
| 114 | +public: |
| 115 | + int minElement(vector<int>& nums) { |
| 116 | + int ans = 100; |
| 117 | + for (int x : nums) { |
| 118 | + int y = 0; |
| 119 | + for (; x > 0; x /= 10) { |
| 120 | + y += x % 10; |
| 121 | + } |
| 122 | + ans = min(ans, y); |
| 123 | + } |
| 124 | + return ans; |
| 125 | + } |
| 126 | +}; |
| 127 | +``` |
| 128 | +
|
| 129 | +#### Go |
| 130 | +
|
| 131 | +```go |
| 132 | +func minElement(nums []int) int { |
| 133 | + ans := 100 |
| 134 | + for _, x := range nums { |
| 135 | + y := 0 |
| 136 | + for ; x > 0; x /= 10 { |
| 137 | + y += x % 10 |
| 138 | + } |
| 139 | + ans = min(ans, y) |
| 140 | + } |
| 141 | + return ans |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +#### TypeScript |
| 146 | + |
| 147 | +```ts |
| 148 | +function minElement(nums: number[]): number { |
| 149 | + let ans: number = 100; |
| 150 | + for (let x of nums) { |
| 151 | + let y = 0; |
| 152 | + for (; x; x = Math.floor(x / 10)) { |
| 153 | + y += x % 10; |
| 154 | + } |
| 155 | + ans = Math.min(ans, y); |
| 156 | + } |
| 157 | + return ans; |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +<!-- tabs:end --> |
| 162 | + |
| 163 | +<!-- solution:end --> |
| 164 | + |
| 165 | +<!-- problem:end --> |
0 commit comments