Skip to content

Commit

Permalink
Create smallest-common-region.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Nov 16, 2019
1 parent eb0b092 commit 8ebf9b1
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Python/smallest-common-region.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Time: O(m * n)
# Space: O(n)

class Solution(object):
def findSmallestRegion(self, regions, region1, region2):
"""
:type regions: List[List[str]]
:type region1: str
:type region2: str
:rtype: str
"""
parents = {region[i] : region[0]
for region in regions
for i in xrange(1, len(region))}
lookup = {region1}
while region1 in parents:
region1 = parents[region1]
lookup.add(region1)
while region2 not in lookup:
region2 = parents[region2]
return region2

0 comments on commit 8ebf9b1

Please sign in to comment.