Skip to content

Commit 8ebf9b1

Browse files
authored
Create smallest-common-region.py
1 parent eb0b092 commit 8ebf9b1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Python/smallest-common-region.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(m * n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def findSmallestRegion(self, regions, region1, region2):
6+
"""
7+
:type regions: List[List[str]]
8+
:type region1: str
9+
:type region2: str
10+
:rtype: str
11+
"""
12+
parents = {region[i] : region[0]
13+
for region in regions
14+
for i in xrange(1, len(region))}
15+
lookup = {region1}
16+
while region1 in parents:
17+
region1 = parents[region1]
18+
lookup.add(region1)
19+
while region2 not in lookup:
20+
region2 = parents[region2]
21+
return region2

0 commit comments

Comments
 (0)