From a899fd4d72b0f3cb2391975427d87e4e0119d1b4 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 29 May 2026 04:52:46 +0000 Subject: [PATCH] feat: add solution for 3300. Minimum Element After Replacement With Digit Sum --- .../analysis_daily_20260529.md | 53 ++++++++++++++++ .../problem.md | 63 +++++++++++++++++++ .../solution_daily_20260529.go | 20 ++++++ .../solution_daily_20260529_test.go | 28 +++++++++ 4 files changed, 164 insertions(+) create mode 100644 problems/3300-minimum-element-after-replacement-with-digit-sum/analysis_daily_20260529.md create mode 100644 problems/3300-minimum-element-after-replacement-with-digit-sum/problem.md create mode 100644 problems/3300-minimum-element-after-replacement-with-digit-sum/solution_daily_20260529.go create mode 100644 problems/3300-minimum-element-after-replacement-with-digit-sum/solution_daily_20260529_test.go diff --git a/problems/3300-minimum-element-after-replacement-with-digit-sum/analysis_daily_20260529.md b/problems/3300-minimum-element-after-replacement-with-digit-sum/analysis_daily_20260529.md new file mode 100644 index 0000000..15e647d --- /dev/null +++ b/problems/3300-minimum-element-after-replacement-with-digit-sum/analysis_daily_20260529.md @@ -0,0 +1,53 @@ +# 3300. Minimum Element After Replacement With Digit Sum + +[LeetCode Link](https://leetcode.com/problems/minimum-element-after-replacement-with-digit-sum/) + +Difficulty: Easy +Topics: Array, Math +Acceptance Rate: 87.1% + +## Hints + +### Hint 1 + +Read the problem carefully. You only need two things: a way to transform each element, and a way to track the smallest transformed value. No fancy data structure is needed here — think about what a single linear pass through the array can accomplish. + +### Hint 2 + +The transformation is "replace a number with the sum of its decimal digits." How do you extract digits from an integer in any language? Repeatedly take the value modulo 10 (the last digit) and divide by 10 (drop the last digit) until the value reaches 0. + +### Hint 3 + +You do not need to actually rewrite the array. Compute the digit sum for each element on the fly, compare it against the running minimum, and update the minimum if smaller. Initialize the minimum to a safely large value (or to the digit sum of the first element) so the very first comparison works correctly. + +## Approach + +Walk the array once. For each element `n`, compute its digit sum by looping: + +1. Set `sum = 0`. +2. While `n > 0`, add `n % 10` to `sum`, then set `n = n / 10`. +3. After the inner loop, `sum` is the digit sum of the original number. + +Keep a `best` variable that holds the minimum digit sum seen so far. Update it whenever the freshly computed `sum` is smaller. After processing every element, return `best`. + +Example walk-through with `nums = [999, 19, 199]`: + +- `999` → `9 + 9 + 9 = 27`, `best = 27`. +- `19` → `1 + 9 = 10`, `best = 10`. +- `199` → `1 + 9 + 9 = 19`, `best` stays `10`. + +Return `10`, matching the expected output. + +This works because the minimum-of-transformed-values is independent of order — we never need to look back at earlier elements, so a single streaming pass is sufficient. + +## Complexity Analysis + +Time Complexity: O(n · d), where `n = len(nums)` and `d` is the number of decimal digits per element. Since `nums[i] <= 10^4`, `d <= 5`, so this is effectively O(n). +Space Complexity: O(1) — only a few integer accumulators are used; the input array is not modified. + +## Edge Cases + +- **Single-element array**: With `len(nums) == 1`, the answer is simply the digit sum of that one element. Initializing `best` correctly (e.g., to the first element's digit sum or to a sentinel) avoids returning a stale default. +- **All single-digit numbers**: When every `nums[i] < 10`, each digit sum equals the original number, so the answer is just `min(nums)`. The same code path handles this without any special case. +- **Numbers with trailing zeros (e.g., 10, 100, 1000)**: The `n % 10` / `n /= 10` loop correctly skips zero contributions; `10` becomes `1`, `100` becomes `1`, etc. +- **Largest allowed value (10000)**: Digit sum is `1`. The loop must continue until `n` is exactly `0`, not `<= 0`, to handle this cleanly. diff --git a/problems/3300-minimum-element-after-replacement-with-digit-sum/problem.md b/problems/3300-minimum-element-after-replacement-with-digit-sum/problem.md new file mode 100644 index 0000000..cc8f3f5 --- /dev/null +++ b/problems/3300-minimum-element-after-replacement-with-digit-sum/problem.md @@ -0,0 +1,63 @@ +--- +number: "3300" +frontend_id: "3300" +title: "Minimum Element After Replacement With Digit Sum" +slug: "minimum-element-after-replacement-with-digit-sum" +difficulty: "Easy" +topics: + - "Array" + - "Math" +acceptance_rate: 8709.0 +is_premium: false +created_at: "2026-05-29T04:51:25.405449+00:00" +fetched_at: "2026-05-29T04:51:25.405449+00:00" +link: "https://leetcode.com/problems/minimum-element-after-replacement-with-digit-sum/" +date: "2026-05-29" +--- + +# 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] <= 104` diff --git a/problems/3300-minimum-element-after-replacement-with-digit-sum/solution_daily_20260529.go b/problems/3300-minimum-element-after-replacement-with-digit-sum/solution_daily_20260529.go new file mode 100644 index 0000000..28a31b7 --- /dev/null +++ b/problems/3300-minimum-element-after-replacement-with-digit-sum/solution_daily_20260529.go @@ -0,0 +1,20 @@ +package main + +// Approach: single linear pass. For each element, compute its decimal digit +// sum by repeated mod-10 / div-10, and track the minimum digit sum seen so +// far. O(n * d) time where d is the number of digits (<= 5 here), O(1) space. + +func minElementDaily20260529(nums []int) int { + best := -1 + for _, n := range nums { + sum := 0 + for n > 0 { + sum += n % 10 + n /= 10 + } + if best == -1 || sum < best { + best = sum + } + } + return best +} diff --git a/problems/3300-minimum-element-after-replacement-with-digit-sum/solution_daily_20260529_test.go b/problems/3300-minimum-element-after-replacement-with-digit-sum/solution_daily_20260529_test.go new file mode 100644 index 0000000..353ddcc --- /dev/null +++ b/problems/3300-minimum-element-after-replacement-with-digit-sum/solution_daily_20260529_test.go @@ -0,0 +1,28 @@ +package main + +import "testing" + +func TestMinElementDaily20260529(t *testing.T) { + tests := []struct { + name string + nums []int + expected int + }{ + {"example 1: mixed two-digit numbers", []int{10, 12, 13, 14}, 1}, + {"example 2: already single digits", []int{1, 2, 3, 4}, 1}, + {"example 3: three-digit numbers", []int{999, 19, 199}, 10}, + {"edge case: single element", []int{7}, 7}, + {"edge case: maximum constraint value", []int{10000}, 1}, + {"edge case: all identical elements", []int{55, 55, 55}, 10}, + {"edge case: minimum hides behind large input", []int{9999, 9999, 100, 9999}, 1}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := minElementDaily20260529(tt.nums) + if got != tt.expected { + t.Errorf("minElementDaily20260529(%v) = %d, want %d", tt.nums, got, tt.expected) + } + }) + } +}