This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# Complete the isValid function below. | |
def isValid(s): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# Complete the minimumAbsoluteDifference function below. | |
def minimumAbsoluteDifference(arr): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# Complete the luckBalance function below. | |
def luckBalance(k, contests): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# Complete the getMinimumCost function below. | |
def getMinimumCost(k, c): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# Complete the maxMin function below. | |
def maxMin(k, arr): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def height(root): | |
# condition to stop recursion | |
if root == None: | |
return -1 | |
# divide and conquer | |
depth_left = height(root.left) | |
depth_right = height(root.right) | |
depth = max(depth_left, depth_right) + 1 | |
# return the final result | |
return depth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lca(root, v1, v2): | |
# compare v1 v2 and get the big and small value | |
if v1 > v2: | |
v_big, v_small = v1, v2 | |
else: | |
v_big, v_small = v2, v1 | |
current = root | |
while True: | |
if current.info > v_big: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def decodeHuff(root, s): | |
current = root | |
result = '' | |
for code in s: | |
if int(code) == 0: | |
current = current.left | |
else: | |
current = current.right | |
if current.left == None and current.right == None: | |
result += current.data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def checkBST(root): | |
def is_BST(root): | |
if root == None: | |
return True, None, None | |
bool_left, min_left, max_left = is_BST(root.left) | |
bool_right, min_right, max_right = is_BST(root.right) | |
if bool_left and bool_right: | |
if root.left==None and root.right==None: | |
return True, root.data, root.data | |
elif root.left==None and not root.right==None: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def whatFlavors(cost, money): | |
cost_dict = {} | |
for i,icost in enumerate(cost): | |
if money-icost in cost_dict: | |
print(str(cost_dict[money-icost]+1) + ' ' + str(i+1)) | |
return | |
else: | |
cost_dict[icost] = i |
OlderNewer