def getHint(self, a: str, b: str) -> str: d1, d2 = di(), di() bulls, cows, ln = 0, 0, len(a) for i in range(ln): if a[i] == b[i]: bulls += 1 else: d1[a[i]] += 1 d2[b[i]] += 1 for i in d2: cows += min(d1[i], d2[i]) return "{0}A{1}B".format(bulls, cows)
def findFrequentTreeSum(self, root: TreeNode) -> List[int]: # Time: O(N) # Space: O(N) def postorder(root): nonlocal d if not root: return False lft = postorder(root.left) rt = postorder(root.right) d[root.val + lft + rt] += 1 return root.val + lft + rt d = di() postorder(root) ans = list() if len(d): maxi = max(d.values()) for k, v in d.items(): if v == maxi: ans.append(k) return ans
def canFormPalindrome(s): d = di(s) cnt = 0 for i in d.values(): cnt += (i % 2) return cnt <= 1
def topKFrequent(self, a: List[int], b: int) -> List[int]: d = di(a) ans = list() for i in d.most_common(b): ans.append(i[0]) return ans
def intersect(self, a: List[int], b: List[int]) -> List[int]: d1 = di(b) ret = list() for i in a: if d1[i]: d1[i] -= 1 ret.append(i) return ret
def check(l, k): c = di(l) if len(l) == 1: return "IMPOSSIBLE" for i in range(len(l)): if c[k - l[i]]: tmp = l.index(k - l[i]) if tmp != i: return i + 1, l.index(k - l[i]) + 1 return "IMPOSSIBLE"
def numPairsDivisibleBy60(self, a: List[int]) -> int: a = [i % 60 for i in a] d = di(a) cnt = 0 for i in d: if i == 0 or i == 30: cnt += d[i] * (d[i] - 1) // 2 elif d[i] > 0 and 60 - i in d: cnt += d[i] * d[60 - i] d[i] = 0 return cnt
def sumOfUnique(self, a: List[int]) -> int: # Time: O(N) # Space: O(N) d = di(a) ret = 0 for i in d: if d[i] == 1: ret += i return ret
def countBalls(self, a: int, b: int) -> int: def solve(i): ret = 0 while i: ret += i % 10 i //= 10 return ret d = di() for i in range(a, b + 1): d[solve(i)] += 1 return max(d.values())
def maxOperations(self, a: List[int], k: int) -> int: d = di(a) cnt = 0 for i in a: if d[i] > 0: if k - i == i: if d[i] > 1: d[i] -= 2 cnt += 1 else: if d[k - i] > 0: d[k - i] -= 1 d[i] -= 1 cnt += 1 return cnt
def solve(s): d = di(s) oddcnt = 0 char = '' for i in d: if d[i] % 2: oddcnt += 1 char = i if oddcnt > 1 or (oddcnt == 1 and len(s) % 2 == 0): return "NO SOLUTION" front = "" back = "" for i in d: t = d[i] // 2 front += (i * t) back = (i * t) + back if len(s) % 2: return front + char + back else: return front + back
def longestZigZag(self, root: TreeNode) -> int: # Time: O(N) # Space: O(N) ret = 0 d = di() def solve(root, flag, cnt): nonlocal ret, d if not root: ret = max(ret, cnt) return ret if d[root]: return d[root] if flag == 'left': d[root] = max(d[root], solve(root.left, 'right', cnt + 1)) else: d[root] = max(d[root], solve(root.right, 'left', cnt + 1)) return d[root] def preorder(root): if not root: return if root.left: solve(root.left, 'right', 0) if root.right: solve(root.right, 'left', 0) preorder(root.left) preorder(root.right) preorder(root) return ret
def lca(root, v1, v2): maxi = root tmp = root s = di(int) while tmp: if tmp.info == v1: break elif tmp.info < v1: tmp = tmp.right elif tmp.info > v1: tmp = tmp.left s[tmp] = 1 tmp = root while tmp: if s[tmp]: maxi = tmp if tmp.info == v2: break elif tmp.info > v2: tmp = tmp.left elif tmp.info < v2: tmp = tmp.right return maxi
from collections import defaultdict as di import math q = int(input()) for case in range(1, q + 1): n, p = [int(x) for x in input().split()] R = [int(x) for x in input().split()] vikt = di(list) for i in range(n): for g in [int(x) for x in input().split()]: tal = g / R[i] a = math.ceil(tal / 1.1) b = math.floor(tal / 0.9) #print("a,b",a,b) if a <= b: vikt[i].append((a, b)) vikt[i].sort() #print(vikt) mat = 0 index = [0] * n for port in range(1000000): for ind in vikt: while index[ind] < len( vikt[ind]) and vikt[ind][index[ind]][1] < port: index[ind] += 1 not_enough_left = False for ind in vikt: if index[ind] == len(vikt[ind]):
def firstUniqChar(self, s: str) -> int: d = di(s) for i in range(len(s)): if d[s[i]] == 1: return i return -1
def isAnagram(self, s: str, t: str) -> bool: return di(s) == di(t)
def uniqueOccurrences(self, a: List[int]) -> bool: d = di(a) return len(d.keys()) == len(set(d.values()))
def __init__(self, root: TreeNode): l = self.helper(root, 0, []) self.d = di(l)
def isIsomorphic(self, a: str, b: str) -> bool: return len(di(a)) == len(di(b))