Playing Hacks and Stuffs!
When I was trying to solve this what I thought was the meaning of maximum digit in both numbers are equal.
was wrong which eventually lead to failed case
Here is the thought process for that:
nums
which holds integer values starting from index 0nums
, such that the maximum digit in both numbers are equal[12, 21, 15, 51]
, the list of sum of digits in the array is {3, 6}
which is formed from [1+2 == 2+1]
and [1+5 == 5+1]
respectively51+15 > 12 + 21
Then I wrote the script:
def getMaxDigits(nums):
hashtable = {}
for i in range(len(nums)):
for j in range(i+1, len(nums)):
a, b = nums[i], int(str(nums[j])[::-1])
if a == b:
val = [int(i) for i in list(str(a))]
sum = 0
for i in range(len( val)):
sum += val[i]
hashtable[a] = sum
return hashtable
def maxSum(nums):
digits = getMaxDigits(nums)
# Incase no value found
if len(digits) == 0:
return -1
maximum = max(list(digits.values()))
for key, value in list(digits.items()):
if value == maximum:
pair = int(str(key)[::-1]) + key
return pair
return -1
nums = [1,2,3,4]
r = maxSum(nums)
print(r)
But it worked for some test case till I reached this [31,25,72,79,74]
The expected output is 146
but my program is returning -1
If you look at it based on my assumption made the return value is right but what’s the issue?
Then I had to understand what the statement meant exactly and came up with another solution
So it means that we’re to find the maximum sum of pair of numbers from the array nums
such that the maximum digit in pair are equal
What that means is this:
Let’s take this array for example [15, 25, 1, 2]
The maximum digit in the number is:
max(15) = 5
max(25) == 5
And the sum of the pair is 40
Since there are no other numbers in the array that meets the condition therefore the result is 40
Here’s my final solve script: link
It isn’t too efficient :(
Here’s a better approach I read from other people solution
Approach:
nums
max_digit
into a listmax_digit
Complexity:
Time complexity: O(nlogn)
Space complexity: O(n)
Code:
class Solution:
def maxSum(self, nums: List[int]) -> int:
d = defaultdict(list)
ans = -1
for each in nums:
max_digit = int(max(str(each)))
d[max_digit].append(each)
for each in d:
if len(d[each]) > 1:
d[each].sort()
ans = max(ans, d[each][-1] + d[each][-2])
return ans
You can see it’s way faster