Find Smallest Common Region
Given a 2D array regions[][] where the first region of each array includes all other regions in that array and two more regions r1 and r2, the task is to return the smallest region that contains both of them.
Note: If you are given regions r1, r2, and r3 such that r1 includes r3, it is guaranteed there is no r2 such that r2 includes r3.
Examples:
Input: regions[][] = {{"Earth", "North America", "South America"}, {"North America", "United States", "Canada"}, {"United States", "New York", "Boston"}, {"Canada", "Ontario", "Quebec"}}, r1 = "Quebec", r2 = "New York"
Output: North America
Explanation:
- The first array states that "North America" and "South America" are in region "Earth".
- The second array states that "United States" and "Canada" are in region "North America".
- The third array states that "New York" and "Boston" are in region "United States".
- The fourth array states that "Ontario" and "Quebec" are in region "Canada".
- So, "New York" is in "United States" which in turn is in "North America" and "Quebec" is in "Canada" which in turn is in "United States" which is also in "North America". Therefore, both lies in "North America".
Input: regions[][] = {{"Earth", "North America", "South America"}, {"North America", "United States", "Canada"}, {"United States", "New York", "Boston"}, {"Canada", "Ontario", "Quebec"}, {"South America", "Brazil"}}, r1 = "Canada", r2 = "South America" Output: Earth
Approach: To solve the problem, follow the below idea:
The task is to find the smallest common region that contains two specified regions from a list of hierarchical regions. The problem can be solved using hashmaps to store the parent child relationship and then tracing the path for the common region.
Step-by-Step Approach:
- To map each region to its parent region, use an unordered_map. This facilitates tracking the route from any location to the source.
- Store each area in an unordered set, tracing region 1's path up to the root. This set will enable us to rapidly determine whether an area is a region1 ancestor.
- Trace the course of region 2 all the way to the root. As you follow the path, see if any regions are already included in the group of ancestors of area 1. The smallest common region comes first in this type of region.
- The first common region found in the traced path of region2 that is also in the traced path of region1 is returned as the smallest common region.
Below is the implementation of the above approach:
#include <bits/stdc++.h>
using namespace std;
string findSmallestRegion(vector<vector<string> >& regions,
string& r1, string& r2)
{
// Create a parent mapping for each region
unordered_map<string, string> parent_map;
for (const auto& region_list : regions) {
for (int i = 1; i < region_list.size(); ++i) {
parent_map[region_list[i]] = region_list[0];
}
}
// Trace the path from region1 to the root
unordered_set<string> path1;
while (parent_map.find(r1) != parent_map.end()) {
path1.insert(r1);
r1 = parent_map[r1];
}
// Include the root
path1.insert(r1);
// Trace the path from region2 to the root and find
// the common ancestor
while (parent_map.find(r2) != parent_map.end()) {
if (path1.find(r2) != path1.end()) {
return r2;
}
r2 = parent_map[r2];
}
return r2;
}
int main()
{
vector<vector<string> > regions
= { { "Earth", "North America", "South America" },
{ "North America", "United States", "Canada" },
{ "United States", "New York", "Boston" },
{ "Canada", "Ontario", "Quebec" } };
string r1 = "Quebec";
string r2 = "New York";
cout << findSmallestRegion(regions, r1, r2) << endl;
return 0;
}
Output
North America Earth
Time Complexity: O(N + H), where N is the total number of regions and H is the height of the deepest path between the two specified regions.
Auxiliary Space: O(N + H)