-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathlowest-common-manager.cpp
More file actions
41 lines (35 loc) · 968 Bytes
/
lowest-common-manager.cpp
File metadata and controls
41 lines (35 loc) · 968 Bytes
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <vector>
using namespace std;
class OrgChart {
public:
char name;
vector<OrgChart*> directReports;
OrgChart(char name) {
this->name = name;
this->directReports = {};
}
void addDirectReports(vector<OrgChart*> directReports);
};
struct LCM {
int reports;
OrgChart* name;
};
LCM getLCM(OrgChart* topManager, OrgChart* reportOne, OrgChart* reportTwo) {
int reports = 0;
if (!topManager) return {0, NULL};
if (topManager == reportOne || topManager == reportTwo) {
reports++;
}
for (OrgChart* dr : topManager->directReports) {
LCM lcm = getLCM(dr, reportOne, reportTwo);
reports += lcm.reports;
if (lcm.reports == 2) {
return {2, lcm.name};
}
}
return {reports, topManager};
}
OrgChart* getLowestCommonManager(OrgChart* topManager, OrgChart* reportOne, OrgChart* reportTwo) {
// Write your code here.
return getLCM(topManager, reportOne, reportTwo).name;
}