3300. Minimum Element After Replacement With Digit Sum LeetCode Solution
In this guide, you will get 3300. Minimum Element After Replacement With Digit Sum LeetCode Solution with the best time and space complexity. The solution to Minimum Element After Replacement With Digit Sum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Element After Replacement With Digit Sum solution in C++
- Minimum Element After Replacement With Digit Sum solution in Java
- Minimum Element After Replacement With Digit Sum solution in Python
- Additional Resources
Problem Statement of 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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
3300. Minimum Element After Replacement With Digit Sum LeetCode Solution in C++
class Solution {
public:
int minElement(vector<int>& nums) {
int ans = INT_MAX;
for (const int num : nums)
ans = min(ans, getDigitSum(num));
return ans;
}
private:
int getDigitSum(int num) {
int digitSum = 0;
while (num > 0) {
digitSum += num % 10;
num /= 10;
}
return digitSum;
}
};
/* code provided by PROGIEZ */
3300. Minimum Element After Replacement With Digit Sum LeetCode Solution in Java
class Solution {
public int minElement(int[] nums) {
int ans = Integer.MAX_VALUE;
for (final int num : nums)
ans = Math.min(ans, getDigitSum(num));
return ans;
}
private int getDigitSum(int num) {
int digitSum = 0;
while (num > 0) {
digitSum += num % 10;
num /= 10;
}
return digitSum;
}
}
// code provided by PROGIEZ
3300. Minimum Element After Replacement With Digit Sum LeetCode Solution in Python
class Solution:
def minElement(self, nums: list[int]) -> int:
return min(sum(map(int, str(num))) for num in nums)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.