-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
49 lines (45 loc) · 1.38 KB
/
Solution.cpp
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
42
43
44
45
46
47
48
49
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
string findSmallestRegion(vector<vector<string>>& regions,
string region1,
string region2) {
// The key insight is the constraint "given r1, r2 and r3 such that r1
// includes r3, it is guaranteed there is no r2 such that r2 includes r3".
// This means that for any given node r3, there is at most a single
// incoming edge to it.
// Therefore, we can model it as a Tree problem, each node has at most one
// parent.
// With this, the problem can be reduced to a Lowest Common Ancestor
// problem.
// Naively, we can encapsulate each region in a TreeNode and build the Tree.
// Can we do better?
std::unordered_map<string, string> parents;
for (const auto& region : regions) {
for (size_t i = 1; i < region.size(); ++i) {
parents[region[i]] = region[0];
}
}
string* x = ®ion1;
string* y = ®ion2;
while (*x != *y) {
auto itx = parents.find(*x);
if (itx == parents.end()) {
// Not in the same subtree
x = ®ion2;
} else {
x = &itx->second;
}
auto ity = parents.find(*y);
if (ity == parents.end()) {
y = ®ion1;
} else {
y = &ity->second;
}
}
return *x;
}
};