commit 14ea57ff2a55311777dcca3221da99f556b13058
parent 69e611af083aca7118ae10a035e1971440dd440e
Author: Jack Sangdahl <jack@pngu.org>
Date: Fri, 29 May 2026 07:47:07 -0600
3300. minimum element after replacement with digit sum
Diffstat:
2 files changed, 51 insertions(+), 0 deletions(-)
diff --git a/min_element_replace_digit_sum.c b/min_element_replace_digit_sum.c
@@ -0,0 +1,31 @@
+// 3300. Minimum Element After Replacement With Digit Sum
+#include "leetcode.h"
+
+/*
+ * 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.
+ */
+
+int minElement(int *nums, int numsSize) {
+ int ans = 36;
+ for (int i = 0; i < numsSize; i++) {
+ int n = nums[i];
+ int a = n / 10, b = n / 100, c = n / 1000, d = n / 10000;
+ ans = fmin(ans, n - 9 * (a + b + c + d));
+ }
+ return ans;
+}
+
+int main() {
+ int n1[] = {10, 12, 13, 14}, n2[] = {1, 2, 3, 4}, n3[] = {999, 19, 199};
+ int r1 = minElement(n1, ARRAY_SIZE(n1));
+ int r2 = minElement(n2, ARRAY_SIZE(n2));
+ int r3 = minElement(n3, ARRAY_SIZE(n3));
+ printf("%d\n", r1);
+ assert(r1 == 1);
+ printf("%d\n", r2);
+ assert(r2 == 1);
+ printf("%d\n", r3);
+ assert(r3 == 10);
+}
diff --git a/min_element_replace_digit_sum.py b/min_element_replace_digit_sum.py
@@ -0,0 +1,20 @@
+# 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.
+"""
+
+
+class Solution:
+ def minElement(self, nums: List[int]) -> int:
+ d = lambda n: sum(int(i) for i in str(n))
+ return min(map(d, nums))
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.minElement(nums=[10, 12, 13, 14]))
+ print(obj.minElement(nums=[1, 2, 3, 4]))
+ print(obj.minElement(nums=[999, 19, 199]))