Skip to content

3079. Find the Sum of Encrypted Integers 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
 public:
  int sumOfEncryptedInt(vector<int>& nums) {
    int ans = 0;

    for (const int num : nums) {
      int maxDigit = 0;
      int base = 0;
      for (int x = num; x > 0; x /= 10) {
        maxDigit = max(maxDigit, x % 10);
        base = base * 10 + 1;
      }
      ans += base * maxDigit;
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
  public int sumOfEncryptedInt(int[] nums) {
    int ans = 0;

    for (final int num : nums) {
      int maxDigit = 0;
      int base = 0;
      for (int x = num; x > 0; x /= 10) {
        maxDigit = Math.max(maxDigit, x % 10);
        base = base * 10 + 1;
      }
      ans += base * maxDigit;
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
  def sumOfEncryptedInt(self, nums: list[int]) -> int:
    def getEncrypted(num: int) -> int:
      maxDigit = 0
      base = 0
      while num > 0:
        maxDigit = max(maxDigit, num % 10)
        base = base * 10 + 1
        num //= 10
      return base * maxDigit

    return sum(getEncrypted(num) for num in nums)