-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1257-smallest-common-region.js
More file actions
56 lines (51 loc) · 1.53 KB
/
1257-smallest-common-region.js
File metadata and controls
56 lines (51 loc) · 1.53 KB
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
50
51
52
53
54
55
56
/**
* 1257. Smallest Common Region
* https://leetcode.com/problems/smallest-common-region/
* Difficulty: Medium
*
* You are given some lists of regions where the first region of each list directly contains
* all other regions in that list.
*
* If a region x contains a region y directly, and region y contains region z directly, then
* region x is said to contain region z indirectly. Note that region x also indirectly contains
* all regions indirectly containd in y.
*
* Naturally, if a region x contains (either directly or indirectly) another region y, then x
* is bigger than or equal to y in size. Also, by definition, a region x contains itself.
*
* Given two regions: region1 and region2, return the smallest region that contains both of them.
*
* It is guaranteed the smallest region exists.
*/
/**
* @param {string[][]} regions
* @param {string} region1
* @param {string} region2
* @return {string}
*/
var findSmallestRegion = function(regions, region1, region2) {
const map = new Map();
for (const region of regions) {
const parent = region[0];
for (let i = 1; i < region.length; i++) {
map.set(region[i], parent);
}
}
const path1 = getPath(region1);
const path2 = getPath(region2);
const set = new Set(path1);
for (const ancestor of path2) {
if (set.has(ancestor)) {
return ancestor;
}
}
function getPath(region) {
const path = [];
let current = region;
while (current) {
path.push(current);
current = map.get(current);
}
return path;
}
};