LeetCode 1257. Smallest Common Region
原题链接在这里:https://leetcode.com/problems/smallest-common-region/
题目:
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. Also by definition a region X contains itself.
Given two regions region1, region2, find out the smallest region that contains both of them.
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.
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^4region1 != region2- All strings consist of English letters and spaces with at most 20 letters.
题解:
With the regions list, we could construct partent HashMap with child pointing to parent.
Maintain all the regions used while finding ancestor of region1.
When finding ancestor of region2, return the first occurance of region that is in used, it would be smallest common region.
Time Complexity: O(n). n = regions.size() * average length. h is height of parent tree.
Space: O(n).
AC java:
class Solution {
public String findSmallestRegion(List<List<String>> regions, String region1, String region2) {
HashMap<String, String> hm = new HashMap<>();
for(List<String> item : regions){
String parent = item.get(0);
for(int i = 1; i<item.size(); i++){
hm.put(item.get(i), parent);
}
}
HashSet<String> used = new HashSet<>();
while(region1 != null){
used.add(region1);
region1 = hm.get(region1);
}
while(!used.contains(region2)){
region2 = hm.get(region2);
}
return region2;
}
}
类似Lowest Common Ancestor of a Binary Tree.
LeetCode 1257. Smallest Common Region的更多相关文章
- 【leetcode】1257. Smallest Common Region
题目如下: You are given some lists of regions where the first region of each list includes all other reg ...
- Smallest Common Multiple-freecodecamp算法题目
Smallest Common Multiple 1.要求 找出能被两个给定参数和它们之间的连续数字整除的最小公倍数. 2.思路 设定一个twoMultiple(a,b)函数,求出输入两个参数的最小公 ...
- [Intermediate Algorithm] - Smallest Common Multiple
题目 找出能被两个给定参数和它们之间的连续数字整除的最小公倍数. 范围是两个数字构成的数组,两个数字不一定按数字顺序排序. 例如对 1 和 3 —— 找出能被 1 和 3 和它们之间所有数字整除的最小 ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- 【leetcode&CN&竞赛】1198.Find Smallest Common Element in All Rows
题目如下: 给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1. 示例: 输入:mat = [ ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] K-th Smallest in Lexicographical Order 字典顺序的第K小数字
Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. N ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
随机推荐
- Javal连载4-注释&class与public class区别
一.Java注释 1.作用:不会编译倒.class文件之中:增强可读性 2.分类: (1)单行注释(只注释当前行):// (2)多行注释: /* 注释 注释 注释 */ (3)javadoc注释 /* ...
- background-size:100% 100% 和 background-size:cover的区别简述
下面我通过给下图背景图添加background-size属性的不同属性值,更直观的显示出100%和cover的区别 下图是添加background-size:100% 100% 后的背景图效果,背 ...
- 【大数据】SparkSql 连接查询中的谓词下推处理 (一)
本文首发于 vivo互联网技术 微信公众号 https://mp.weixin.qq.com/s/YPN85WBNcnhk8xKjTPTa2g 作者:李勇 目录: 1.SparkSql 2.连接查询和 ...
- Java & PHP RSA 互通密钥、签名、验签、加密、解密
RSA加密算法是一种非对称加密算法.在公开密钥加密和电子商业中RSA被广泛使用.RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Le ...
- JVM的监控工具之jvisual
VisualVM(All-in-One Java Trouble shootingTool)是到目前为止随JDK发布的功能最强大的运行监视和故障处理程序,并且可以预见在未来一段时间内都是官方主力发展的 ...
- Python - 标准库概况 - 第二十一天
Python 标准库概览 操作系统接口 os模块提供了不少与操作系统相关联的函数. 建议使用 "import os" 风格而非 "from os import *&quo ...
- 先排序然后union all失效,mysql数据库多个表union all查询并排序的结果为什么错误
mysql数据库多个表union all查询并排序的结果为什么错误? 群主,我想进行一个表的查询,先把表中某个字段的内容查出,然后其他的再排序,我用union all连接两个表的查询结果排序是错的 比 ...
- SQL之CASE WHEN用法详解
原文链接:https://blog.csdn.net/rongtaoup/article/details/82183743 原文链接:https://www.cnblogs.com/zhuyeshen ...
- dtd语法
dtd语法 <!ELEMENT 元素名 约束> //简单元素三种:没有子元素的元素 eg: <!ELEMENT name (#PCDATA)> (#PCDATA):约束name ...
- 在 React 项目中引入 GG-Editor 编辑可视化流程
蚂蚁金服数据可视化团队曾经开源了一款G6-Editor,但后来停止了对外支持,学习成本太高 好在后来他们团队的大牛高力结合 React + G6 开源了一个 GG-Editor(其实就是G6-Edit ...