LeetCode 3079: Find the Sum of Encrypted Integers (Max-Digit Repeat Construction)
LeetCode 3079ArrayMathDigit ProcessingToday we solve LeetCode 3079 - Find the Sum of Encrypted Integers.
Source: https://leetcode.com/problems/find-the-sum-of-encrypted-integers/
English
Problem Summary
For each integer in nums, replace it with an encrypted value formed by repeating its maximum digit for the same number of digits. Return the sum of all encrypted values.
Key Insight
Each number can be processed independently: while scanning digits, track maxDigit and digitCount. Then construct encrypted value as maxDigit repeated digitCount times.
Brute Force and Limitations
A string-based approach (convert number to string, find max char, build repeated string, parse back) works but adds conversion overhead. Integer math is cleaner and faster.
Optimal Algorithm Steps
1) Initialize total sum = 0.
2) For each number, scan digits to get maxDigit and digitCount.
3) Build encrypted number by repeating maxDigit exactly digitCount times.
4) Add encrypted number to sum and return final sum.
Complexity Analysis
Time: O(n * d), where d is digits per number (at most 4 under constraints, effectively linear).
Space: O(1).
Common Pitfalls
- Forgetting single-digit numbers should keep one digit.
- Not handling 0 safely in a generic helper.
- Mixing “maximum digit value” with “most frequent digit”.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int sumOfEncryptedInt(int[] nums) {
int ans = 0;
for (int x : nums) {
ans += encrypt(x);
}
return ans;
}
private int encrypt(int x) {
if (x == 0) return 0;
int maxDigit = 0;
int digits = 0;
int t = x;
while (t > 0) {
maxDigit = Math.max(maxDigit, t % 10);
digits++;
t /= 10;
}
int encrypted = 0;
for (int i = 0; i < digits; i++) {
encrypted = encrypted * 10 + maxDigit;
}
return encrypted;
}
}func sumOfEncryptedInt(nums []int) int {
ans := 0
for _, x := range nums {
ans += encrypt(x)
}
return ans
}
func encrypt(x int) int {
if x == 0 {
return 0
}
maxDigit, digits := 0, 0
for t := x; t > 0; t /= 10 {
d := t % 10
if d > maxDigit {
maxDigit = d
}
digits++
}
encrypted := 0
for i := 0; i < digits; i++ {
encrypted = encrypted*10 + maxDigit
}
return encrypted
}class Solution {
public:
int sumOfEncryptedInt(vector<int>& nums) {
int ans = 0;
for (int x : nums) {
ans += encrypt(x);
}
return ans;
}
private:
int encrypt(int x) {
if (x == 0) return 0;
int maxDigit = 0, digits = 0;
int t = x;
while (t > 0) {
maxDigit = max(maxDigit, t % 10);
digits++;
t /= 10;
}
int encrypted = 0;
for (int i = 0; i < digits; i++) {
encrypted = encrypted * 10 + maxDigit;
}
return encrypted;
}
};class Solution:
def sumOfEncryptedInt(self, nums: list[int]) -> int:
return sum(self._encrypt(x) for x in nums)
def _encrypt(self, x: int) -> int:
if x == 0:
return 0
max_digit = 0
digits = 0
t = x
while t > 0:
max_digit = max(max_digit, t % 10)
digits += 1
t //= 10
encrypted = 0
for _ in range(digits):
encrypted = encrypted * 10 + max_digit
return encryptedvar sumOfEncryptedInt = function(nums) {
let ans = 0;
for (const x of nums) {
ans += encrypt(x);
}
return ans;
};
function encrypt(x) {
if (x === 0) return 0;
let maxDigit = 0;
let digits = 0;
for (let t = x; t > 0; t = Math.floor(t / 10)) {
const d = t % 10;
if (d > maxDigit) maxDigit = d;
digits++;
}
let encrypted = 0;
for (let i = 0; i < digits; i++) {
encrypted = encrypted * 10 + maxDigit;
}
return encrypted;
}中文
题目概述
给定数组 nums,把每个整数加密为:取该数中的最大数字,并按原位数重复拼接成新数。返回所有加密数之和。
核心思路
每个数字互不影响。对单个数字做一遍“取位”扫描,拿到最大数字和位数;再按位数把最大数字重复拼接即可。
暴力解法与不足
可以先转字符串再处理(找最大字符、重复构造字符串、再转回整数),但这种写法多了不必要的转换。直接用整数运算更稳更快。
最优算法步骤
1)初始化总和 sum = 0。
2)遍历 nums,对每个数统计 maxDigit 与位数 digits。
3)将 maxDigit 重复 digits 次构造加密值。
4)累加后返回总和。
复杂度分析
时间复杂度:O(n * d),其中 d 为每个数的位数(题目约束下很小)。
空间复杂度:O(1)。
常见陷阱
- 单位数也要按 1 位处理。
- 通用函数里忘记处理 0。
- 把“最大数字”误写成“出现次数最多的数字”。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int sumOfEncryptedInt(int[] nums) {
int ans = 0;
for (int x : nums) {
ans += encrypt(x);
}
return ans;
}
private int encrypt(int x) {
if (x == 0) return 0;
int maxDigit = 0;
int digits = 0;
int t = x;
while (t > 0) {
maxDigit = Math.max(maxDigit, t % 10);
digits++;
t /= 10;
}
int encrypted = 0;
for (int i = 0; i < digits; i++) {
encrypted = encrypted * 10 + maxDigit;
}
return encrypted;
}
}func sumOfEncryptedInt(nums []int) int {
ans := 0
for _, x := range nums {
ans += encrypt(x)
}
return ans
}
func encrypt(x int) int {
if x == 0 {
return 0
}
maxDigit, digits := 0, 0
for t := x; t > 0; t /= 10 {
d := t % 10
if d > maxDigit {
maxDigit = d
}
digits++
}
encrypted := 0
for i := 0; i < digits; i++ {
encrypted = encrypted*10 + maxDigit
}
return encrypted
}class Solution {
public:
int sumOfEncryptedInt(vector<int>& nums) {
int ans = 0;
for (int x : nums) {
ans += encrypt(x);
}
return ans;
}
private:
int encrypt(int x) {
if (x == 0) return 0;
int maxDigit = 0, digits = 0;
int t = x;
while (t > 0) {
maxDigit = max(maxDigit, t % 10);
digits++;
t /= 10;
}
int encrypted = 0;
for (int i = 0; i < digits; i++) {
encrypted = encrypted * 10 + maxDigit;
}
return encrypted;
}
};class Solution:
def sumOfEncryptedInt(self, nums: list[int]) -> int:
return sum(self._encrypt(x) for x in nums)
def _encrypt(self, x: int) -> int:
if x == 0:
return 0
max_digit = 0
digits = 0
t = x
while t > 0:
max_digit = max(max_digit, t % 10)
digits += 1
t //= 10
encrypted = 0
for _ in range(digits):
encrypted = encrypted * 10 + max_digit
return encryptedvar sumOfEncryptedInt = function(nums) {
let ans = 0;
for (const x of nums) {
ans += encrypt(x);
}
return ans;
};
function encrypt(x) {
if (x === 0) return 0;
let maxDigit = 0;
let digits = 0;
for (let t = x; t > 0; t = Math.floor(t / 10)) {
const d = t % 10;
if (d > maxDigit) maxDigit = d;
digits++;
}
let encrypted = 0;
for (let i = 0; i < digits; i++) {
encrypted = encrypted * 10 + maxDigit;
}
return encrypted;
}
Comments