Smallest Common Region Problem
Description
LeetCode Problem 1257.
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.
Example 1:
Input:
regions = [["Earth","North America","South America"],
["North America","United States","Canada"],
["United States","New York","Boston"],
["Canada","Ontario","Quebec"],
["South America","Brazil"]],
region1 = "Quebec",
region2 = "New York"
Output: "North America"
Example 2:
Input: regions = [["Earth", "North America", "South America"],["North America", "United States", "Canada"],["United States", "New York", "Boston"],["Canada", "Ontario", "Quebec"],["South America", "Brazil"]], region1 = "Canada", region2 = "South America"
Output: "Earth"
Constraints:
2 <= regions.length <= 10^42 <= regions[i].length <= 201 <= regions[i][j].length, region1.length, region2.length <= 20region1 != region2regions[i][j],region1, andregion2consist of English letters.- The input is generated such that there exists a region which contains all the other regions, either directly or indirectly.
- A region cannot be directly contained in more than one region.
Solution
Approach: Walk Up from Both Regions
The regions form a tree: the first region of each list is the parent of the others, every region has at most one parent, and one region contains all the rest. The smallest region containing both region1 and region2 is their lowest common ancestor, where a region counts as its own ancestor.
- Build a map
parentfrom each region to the region that directly contains it. - Walk up from
region1to the root, putting every region on the way (includingregion1) into a setancestors. - Walk up from
region2. The first region that is inancestorscontains both, and since we go up fromregion2one step at a time, it is the smallest one.
Starting each walk at the region itself handles the case where one region contains the other: in Example 2, "Earth" is reached from "South America" after one step, and it is already in the set built from "Canada". If region2 were itself an ancestor of region1, the second walk would stop right away.
Complexity: O(N) time and space, where N is the total number of region names in regions.
class Solution:
def findSmallestRegion(self, regions: List[List[str]], region1: str, region2: str) -> str:
parent = {} # region -> the region that directly contains it
for r in regions:
for child in r[1:]:
parent[child] = r[0]
# Every region that contains region1, from region1 itself up to the root.
ancestors = set()
x = region1
while True:
ancestors.add(x)
if x not in parent:
break
x = parent[x]
# The first of them on the way up from region2 is the smallest common region.
x = region2
while x not in ancestors:
x = parent[x]
return x