Skip to content

Instantly share code, notes, and snippets.

@sdasara95
Last active May 24, 2020 01:26
Show Gist options
  • Save sdasara95/7a43c7b72645afe2b3243cd4671485cb to your computer and use it in GitHub Desktop.
Save sdasara95/7a43c7b72645afe2b3243cd4671485cb to your computer and use it in GitHub Desktop.
Maximum applications when alternative only allowed
# https://docs.google.com/document/d/1EPMkzF8NwoE6xYdFw_RuJKseo4wEvzw6P6jRI_XqtSE/edit
class BinaryTree(object):
def __init__(self, value):
self.value = value
self.left = None
self.right = None
'''
Job Applications
You've found yourself in the role of a job applicant. You have a list of recruiters at a large company you need to contact. After some preliminary analysis, being the adept applicant you are, you figure out that the recruiter information is stored in the form of a Binary tree! Besides the root, each recruiter has one and only one parent node. All directly linked recruiters keep tabs on each other and they will automatically discard your application if you apply to both of them. Each recruiter also has an associated number of positions (node value) that they can send your application to (Assume all recruiters have a unique list of positions).
Determine the maximum positions you can get your application sent to, without having your application discarded.
3
/ \
500 5
/ \ \
1 3000 100
Ans- 3104
'''
#inp = [3,4,5,1,3,null,1]
root = BinaryTree(3)
root.left = BinaryTree(500)
root.left.left = BinaryTree(1)
root.left.right =BinaryTree(3)
root.right = BinaryTree(5)
root.right.right = BinaryTree(100)
org = root
def max_apps(root, can_use_root):
if root is None:
return 0
if can_use_root:
use_root = root.value + max_apps(root.right, False) + max_apps(root.left, False)
skip_root = max_apps(root.right, True) + max_apps(root.left, True)
else:
return max_apps(root.right, True) + max_apps(root.left, True)
return max(use_root, skip_root)
print(max_apps(org,True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment