classSolution: deftopKFrequent(self, nums: List[int], k: int) -> List[int]: freq_dict = {} for i in nums: if i in freq_dict.keys(): freq_dict[i] += 1 else: freq_dict[i] = 1 nums = [] for i, v in freq_dict.items(): nums.append((v, i))
nums.sort(key=lambda x: x[0], reverse=True)
out = [] for counts, idx in nums: iflen(out) == k: break out.append(idx)
start = 0 for v in arr2: while (v in arr1[start:]): idx = arr1[start:].index(v) + start temp = arr1[start] arr1[start] = arr1[idx] arr1[idx] = temp start += 1
out = arr1[:start] out.extend(sorted(arr1[start:]))
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: defisSymmetric(self, root: TreeNode) -> bool:
defsymmetric(root_1, root_2): ifnot root_1 andnot root_2: returnTrue else: if (not root_1 and root_2) or (root_1 andnot root_2): returnFalse elif root_1.val != root_2.val: returnFalse else: check_left = symmetric(root_1.left, root_2.right) check_right = symmetric(root_1.right, root_2.left) return check_left and check_right
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: deflevelOrder(self, root: TreeNode) -> List[List[int]]: ifnot root: return [] q = [[root]] levels = [] while q: head = q.pop() next_nodes = [] curr_level = [] for i in head: if i.left: next_nodes.append(i.left)
if i.right: next_nodes.append(i.right) curr_level.append(i.val)
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution_1: defisValidBST(self, root: TreeNode) -> bool:
defis_valid(root, lower=float('-inf'), upper=float('inf')): ifnot root: returnTrue elif root.val >= upper or root.val <= lower: returnFalse else: left = is_valid(root.left, lower, root.val) right = is_valid(root.right, root.val, upper)
return left and right return is_valid(root)
classSolution_2: defisValidBST(self, root: TreeNode) -> bool: deftree(node): ifnot node: return tree(node.left) ls.append(node.val) tree(node.right) ls = [] tree(root) for i inrange(len(ls)-1): if ls[i] >= ls[i+1]: returnFalse returnTrue
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: defzigzagLevelOrder(self, root: TreeNode) -> List[List[int]]: ifnot root: return []
q = [[root]] i = 0 out_lst = [[root.val]] whileTrue: nodes = q.pop(0) temp_lst = [] for j inrange(len(nodes) - 1, -1, -1): if i % 2 == 0: if nodes[j].right: temp_lst.append(nodes[j].right)
if nodes[j].left: temp_lst.append(nodes[j].left) else: if nodes[j].left: temp_lst.append(nodes[j].left) if nodes[j].right: temp_lst.append(nodes[j].right)
ifnot temp_lst: break
i += 1 q.append(temp_lst) out_lst.append([k.val for k in temp_lst]) return out_lst
classSolution: deforangesRotting(self, grid: List[List[int]]) -> int: n, d = len(grid), len(grid[0]) q = [] counts = 0 for i inrange(n): for j inrange(d): if grid[i][j] == 1: counts += 1
if grid[i][j] == 2: q.append((i, j))
rounds = 0 while q and (counts > 0): for _ inrange(len(q)): i, j = q.pop(0) # check up if i != 0: if grid[i - 1][j] == 1: grid[i - 1][j] = 2 q.append((i - 1, j)) counts -= 1
# check down if i != (n - 1): if grid[i + 1][j] == 1: grid[i + 1][j] = 2 q.append((i + 1, j)) counts -= 1
# check left if j != 0: if grid[i][j - 1] == 1: grid[i][j - 1] = 2 q.append((i, j - 1)) counts -= 1
# check right if j != (d - 1): if grid[i][j + 1] == 1: grid[i][j + 1] = 2 q.append((i, j + 1)) counts -= 1
rounds += 1 for i inrange(n): for j inrange(d): if grid[i][j] == 1: return -1
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right classSolution: defflatten(self, root: TreeNode) -> None: """ Do not return anything, modify root in-place instead. """
deflinked_lst_tree(root): ifnot root: returnNone
ifnot root.left andnot root.right: return root
left = linked_lst_tree(root.left) right = linked_lst_tree(root.right) if left: temp = left while temp.right: temp = temp.right
temp.right = right root.right = left root.left = None