Smallest Common Region - Problem
Imagine a hierarchical system of regions where larger regions contain smaller ones, like countries containing states, states containing cities. You're given several lists where the first region in each list directly contains all other regions in that list.
The containment relationship works transitively: if region X contains region Y, and region Y contains region Z, then X indirectly contains Z. By definition, every region contains itself.
Your task: Given two specific regions, find the smallest region that contains both of them. This is essentially finding the Lowest Common Ancestor (LCA) in a tree structure.
Example: If you have regions like
The containment relationship works transitively: if region X contains region Y, and region Y contains region Z, then X indirectly contains Z. By definition, every region contains itself.
Your task: Given two specific regions, find the smallest region that contains both of them. This is essentially finding the Lowest Common Ancestor (LCA) in a tree structure.
Example: If you have regions like
["Earth", "North America", "USA"] and ["North America", "Canada", "Ontario"], and you're looking for the smallest common region containing both "USA" and "Ontario", the answer would be "North America". Input & Output
example_1.py โ Basic Tree Structure
$
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"
๐ก Note:
Quebec is in Canada, New York is in United States. Both Canada and United States are in North America, which is the smallest region containing both Quebec and New York.
example_2.py โ Same Parent
$
Input:
regions = [["Earth", "North America", "South America"], ["North America", "United States", "Canada"]], region1 = "United States", region2 = "Canada"
โบ
Output:
"North America"
๐ก Note:
Both United States and Canada are direct children of North America, so North America is their lowest common ancestor.
example_3.py โ One Contains Other
$
Input:
regions = [["Earth", "North America", "South America"], ["North America", "United States", "Canada"], ["United States", "New York", "Boston"]], region1 = "United States", region2 = "New York"
โบ
Output:
"United States"
๐ก Note:
Since United States directly contains New York, United States itself is the smallest common region containing both.
Constraints
- 2 โค regions.length โค 104
- 2 โค regions[i].length โค 20
- 1 โค regions[i][j].length, region1.length, region2.length โค 20
- regions[i][j], region1, and region2 consist of English letters and spaces only
- It's guaranteed that the smallest region exists
- All regions form a valid tree structure
Visualization
Tap to expand
Understanding the Visualization
1
Parse Region Lists
Convert input lists into parent-child relationships
2
Build Tree Structure
Create a mapping where each region points to its parent
3
Mark First Path
Trace from region1 to root, marking all ancestors
4
Find Intersection
Trace from region2 upward until hitting a marked ancestor
5
Return LCA
The first marked ancestor encountered is the answer
Key Takeaway
๐ฏ Key Insight: Convert the region containment problem into a tree structure where finding the smallest common region becomes finding the Lowest Common Ancestor (LCA) - a classic tree algorithm!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code