forked from lupamo3-zz/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lowest_Common_Manager.py
26 lines (21 loc) · 1004 Bytes
/
Lowest_Common_Manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class OrgInfo:
def __init__(self, lowestCommonManager, numImportantReports):
self.lowestCommonManager = lowestCommonManager
self.numImportantReports = numImportantReports
class Node:
def __init__(self, directReports):
self.directReports = directReports
# O(n) time | O(d) spaces
def getLowestCommonManager(topManager, reportOne, reportTwo):
return getOrgInfo(topManager, reportOne, reportTwo).lowestCommonManager
def getOrgInfo(manager: Node, reportOne, reportTwo):
numImportantReports = 0
for directReport in manager.directReports:
orgInfo = getOrgInfo(directReport, reportOne, reportTwo)
if orgInfo.lowestCommonManager is not None:
return orgInfo
numImportantReports += orgInfo.numImportantReports
if manager == reportOne or manager == reportTwo:
numImportantReports += 1
lowestCommonManager = manager if numImportantReports == 2 else None
return OrgInfo(lowestCommonManager, numImportantReports)