we need to define a secondary function encrypt that can replcae every digit in an integer with the largest digit
the sumOfEncrypted Integers function needs to take all of these and return the sum
- convert the integer into a string, take its max element
- create a new string with all characters == to the max element
- return it as an integer
- iterate through nums and sum the ecrypted integers
π€What Did I Struggle With?
- reading the question after the mock, i realise the question was not explained correctly to me at all
π‘ Explanation of Solution
same as initial thoughts
β Complexity Analysis
Time Complexity:
encrypt() : O(d) where d is the number of digits in a number
sumOfEncryptedInt : (n * d) as we are calling the encrypt function N times
Space Complexity: O(d) where we have an additional string for each integer containing d digits
π» Implementation of Solution
class Solution {public:Β Β int sumOfEncryptedInt(vector<int>& nums) {Β Β Β Β // lambda function to encrypt a single numberΒ Β Β Β auto encrypt = [](int x) {Β Β Β Β Β Β string digits = to_string(x);Β Β Β Β Β Β char max_digit = *max_element(digits.begin(), digits.end()); // Find the largest digitΒ Β Β Β Β Β string encrypted(digits.size(), max_digit); // Create the encrypted stringΒ Β Β Β Β Β return stoi(encrypted);Β Β Β Β };Β Β Β Β int encryptedSum = 0;Β Β Β Β for(int num : nums) {Β Β Β Β Β Β encryptedSum += encrypt(num);Β Β Β Β }Β Β Β Β return encryptedSum;Β Β }};