|
| 1 | +--- |
| 2 | +description: "Author: @wingkwong | https://leetcode.com/problems/minimum-element-after-replacement-with-digit-sum/" |
| 3 | +--- |
| 4 | + |
| 5 | +# 3300 - Minimum Element After Replacement With Digit Sum (Easy) |
| 6 | + |
| 7 | +## Problem Link |
| 8 | + |
| 9 | +https://leetcode.com/problems/minimum-element-after-replacement-with-digit-sum/ |
| 10 | + |
| 11 | +## Problem Statement |
| 12 | + |
| 13 | +You are given an integer array `nums`. |
| 14 | + |
| 15 | +You replace each element in `nums` with the **sum** of its digits. |
| 16 | + |
| 17 | +Return the **minimum** element in `nums` after all replacements. |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | +**Input:** nums = [10,12,13,14] |
| 22 | + |
| 23 | +**Output:** 1 |
| 24 | + |
| 25 | +**Explanation:** |
| 26 | + |
| 27 | +`nums` becomes `[1, 3, 4, 5]` after all replacements, with minimum element 1. |
| 28 | + |
| 29 | +**Example 2:** |
| 30 | + |
| 31 | +**Input:** nums = [1,2,3,4] |
| 32 | + |
| 33 | +**Output:** 1 |
| 34 | + |
| 35 | +**Explanation:** |
| 36 | + |
| 37 | +`nums` becomes `[1, 2, 3, 4]` after all replacements, with minimum element 1. |
| 38 | + |
| 39 | +**Example 3:** |
| 40 | + |
| 41 | +**Input:** nums = [999,19,199] |
| 42 | + |
| 43 | +**Output:** 10 |
| 44 | + |
| 45 | +**Explanation:** |
| 46 | + |
| 47 | +`nums` becomes `[27, 10, 19]` after all replacements, with minimum element 10. |
| 48 | + |
| 49 | +**Constraints:** |
| 50 | + |
| 51 | +- `1 <= nums.length <= 100` |
| 52 | +- `1 <= nums[i] <= 10^4` |
| 53 | + |
| 54 | +## Approach 1: Brute Force |
| 55 | + |
| 56 | +For each element, we calculate the digit sum and use $res$ to keep track of the minimum one. |
| 57 | + |
| 58 | +<Tabs> |
| 59 | +<TabItem value="py" label="Python"> |
| 60 | +<SolutionAuthor name="@wingkwong"/> |
| 61 | + |
| 62 | +```py |
| 63 | +class Solution: |
| 64 | + def minElement(self, nums: List[int]) -> int: |
| 65 | + res = 10 ** 9 |
| 66 | + for x in nums: |
| 67 | + s = 0 |
| 68 | + while x > 0: |
| 69 | + s += x % 10 |
| 70 | + x //= 10 |
| 71 | + res = min(res, s) |
| 72 | + return res |
| 73 | +``` |
| 74 | + |
| 75 | +</TabItem> |
| 76 | +</Tabs> |
0 commit comments