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 ["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
Region Hierarchy โ†’ Tree StructureInput Lists:["Earth", "North America", "South America"]["North America", "USA", "Canada"]["USA", "California", "New York"]["Canada", "Ontario", "Quebec"]ConvertEarthN.AmericaS.AmericaUSACanadaCANYONQCFind LCA of California & Quebec:โœ“ LCA: North America
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!
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
24.3K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen