Panda Guru LogoPanda
Guru

Can you help me to determine its time complexity? - Leetcode Today POD

Round 1

Questions: Can you help determine its time complexity? (Accepted)
Q. Problem link - Max Sum of a Pair with Equal Sum of Digits

class Solution: # function for sum of digits of a number def checkDigitSum(self, n): total=0 while n!=0: rem=n%10 total+=rem n=n//10 return total def maximumSum(self, nums: List[int]) -> int: n=len(nums) result=-1 d={} for i in nums: digitSum=self.checkDigitSum(i) if digitSum not in d: d[digitSum]=[i, -1] else: if d[digitSum][1] == -1: if d[digitSum][0] < i: d[digitSum][1]=i else: d[digitSum][1]=d[digitSum][0] d[digitSum][0]=i else: # if both (0 and 1) < i if i > d[digitSum][0] and i > d[digitSum][1]: d[digitSum][0]=d[digitSum][1] d[digitSum][1]=i elif i > d[digitSum][0] and i < d[digitSum][1]: d[digitSum][0]=i for key, values in d.items(): a=values[0] b=values[1] if a>-1 and b>-1: result=max(result, a+b) return result
Candidate's Approach

The candidate implemented a function to calculate the sum of digits of a number and used a dictionary to store pairs of numbers with the same digit sum. The approach involves iterating through the list of numbers, calculating their digit sums, and updating the dictionary accordingly to keep track of the two largest numbers for each digit sum. Finally, the candidate computes the maximum sum of pairs with equal digit sums.

Interviewer's Feedback

No feedback provided.