题目如下:

You are given some lists of regions where the first region of each list includes all other regions in that list.

Naturally, if a region X contains another region Y then X is bigger than Y.

Given two regions region1region2, find out the smallest region that contains both of them.

If you are given regions r1r2 and r3 such that r1 includes r3, it is guaranteed there is no r2 such that r2 includes r3.

It's guaranteed the smallest region exists.

Example 1:

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"

Constraints:

  • 2 <= regions.length <= 10^4
  • region1 != region2
  • All strings consist of English letters and spaces with at most 20 letters.

解题思路:首先递归找出region1所属的regions链,并保持结果;然后再递归查找region2所属的regions链,找到第一个region在region1所属的regions链即可。

代码如下:

class Solution(object):
def findSmallestRegion(self, regions, region1, region2):
"""
:type regions: List[List[str]]
:type region1: str
:type region2: str
:rtype: str
"""
dic = {}
for region in regions:
parent = region[0]
for i in range(1,len(region)):
dic[region[i]] = parent dic_region1 = {}
while region1 in dic:
dic_region1[region1] = 1
region1 = dic[region1] while region2 in dic:
if region2 in dic_region1:
return region2
region2 = dic[region2]
return regions[0][0]

【leetcode】1257. Smallest Common Region的更多相关文章

  1. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. LeetCode 1257. Smallest Common Region

    原题链接在这里:https://leetcode.com/problems/smallest-common-region/ 题目: You are given some lists of region ...

  3. 【leetcode】302.Smallest Rectangle Enclosing Black Pixels

    原题 An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The bl ...

  4. 【leetcode】1081. Smallest Subsequence of Distinct Characters

    题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...

  5. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...

  6. 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  8. 【LeetCode】910. Smallest Range II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

随机推荐

  1. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    1.nginx 配置模板 server { listen ; client_max_body_size 512M; proxy_set_header Connection ""; ...

  2. Java中对比单继承与多继承的优劣,以及java的解决方案

    继承是一种面相对象的基本特征之一,但在具体语言的语法设计中有两种方式:单继承和多继承. 所谓多继承是指一个子类可以拥有多个父类:单继承则是一个子类只拥有一个父类. 单继承与多继承的优劣: 多继承优点在 ...

  3. WIN32_FIND_DATA

    基本信息 编辑 关于文件的全部属性信息.总计有以下以下9种:文件的标题名.文件的属性(只读.存档,隐藏等).文件的创建时间.文件的最后访问时间.文件的最后修改时间.文件大小的高位双字.文件大小的低位双 ...

  4. Oracle-DML- insert & update & delete

    说明:语句中说到的“表”,以及表中有哪些“列”自行脑补......重要的是理解概率,能看懂语句代表的含义就OK~ DML-数据操作语句: 1. insert 新增 /*insert into 表名va ...

  5. JAVA验证

    1.一个JAVA类只能有一个主类. 2.main()方法返回值改为int,不能运行 3.变量作用域有限 实例: 4.数值类型在内存中直接存储其本身的值,对于不同的数值类型,内存中会分配相应的大小去存储 ...

  6. jQuery 实现图片放大两种方式

    jQuery 实现图片放大两种方式 一.利用css样式表实现,多用于后台显示 1.这种比较简单,利用dom元素的hover实现样式切换 <style> img{ cursor: point ...

  7. 基于Hadoop伪分布式集群搭建Spark

    一.前置安装 1)JDK 2)Hadoop伪分布式集群 二.Scala安装 1)解压Scala安装包 2)环境变量 SCALA_HOME = C:\ProgramData\scala-2.10.6 P ...

  8. Spring 中的bean 是线程安全的吗?

    结论: 不是线程安全的 Spring容器中的Bean是否线程安全,容器本身并没有提供Bean的线程安全策略,因此可以说Spring容器中的Bean本身不具备线程安全的特性,但是具体还是要结合具体sco ...

  9. command----常用命令更新ing

    common commands 1:split---拆分文件 [root@localhost split]# split -b 1M split.tar.gz split_ #按1M拆分文件 [roo ...

  10. windows下php配置环境变量

    这样重启终端后,通过php -v可查看新使用的php版本信息. 注:在path配置在上方的先生效