# A. Maximum Number of Pairs in Array (opens new window)
For each distinct number, count its frequency F
, add A[0] by F//2
, add A[1] by F%2
.
DETAILS
def numberOfPairs(self, A: List[int]) -> List[int]:
ans = [0, 0]
C = collections.Counter(A)
for a in C.keys():
ans[0] += C[a] // 2
ans[1] += C[a] % 2
return ans
# B. Max Sum of a Pair With Equal Sum of Digits (opens new window)
DETAILS
def maximumSum(self, A: List[int]) -> int:
ans = collections.defaultdict(list)
res = -1
for a in A:
curr = sum(map(int, list(str(a))))
ans[curr].append(a)
ans[curr] = sorted(ans[curr])[-2:]
for a in ans.keys():
if len(ans[a]) == 2:
res = max(res, sum(ans[a]))
return res
# C. Query Kth Smallest Trimmed Number (opens new window)
Trim each number as requested, sorted the number according with their original index and get the certain one.
DETAILS
def smallestTrimmedNumbers(self, A: List[str], Q: List[List[int]]) -> List[int]:
return [sorted((a[-d:], i) for i, a in enumerate(A))[r - 1][1] for r, d in Q]
# D. Minimum Deletions to Make Array Divisible (opens new window)
Get the gcd d
of D
, sort array A
and find the smallest number from A
that divided by d
.
DETAILS
def minOperations(self, A: List[int], D: List[int]) -> int:
d = functools.reduce(lambda x, y: gcd(x, y), D)
A.sort()
for i in range(len(A)):
if d % A[i] == 0:
return i
return -1