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 ...
随机推荐
- jQuery与IE兼容性问题处理
jQuery虽然是兼容所有主流的浏览器,但实际应用中也会存在很多兼容性的问题.使用IE11+jquery-3.2.1.min.js遇到的问题如: 对象不支持indexOf 在低版本中,1.8写法如下: ...
- 《计算机网络第7版》PDF+《计算机网络释疑与习题解答第7版》PDF
谢希仁 链接:https://pan.baidu.com/s/1_sM9bFL0y3S1NXBz5rLyvg 提取码:po3i 计算机网络(第7版)谢希仁 · 语雀 https://www.yuque ...
- 修改官方发行openstack镜像的cloud-init登录方式为账号密码登录
openstack使用的镜像多为qcow2格式,各个发行商也开源了针对openstack制作的镜像.但是这些镜像的登录方式都是注入用户名和密码的方式,就是说不能够直接通过账号和密码登录.那么如何将一个 ...
- Token认证,如何快速方便获取用户信息
背景 我们有一个Web项目,这个项目提供了很多的Rest API.也做了权限控制,访问API的请求必须要带上事先认证后获取的Token才可以. 认证的话就在Filter中进行的,会获取请求的Token ...
- Chrome保存整个网页为图片
打开需要保存为图片的网页 然后按F12,接着按Ctrl+Shift+P 在红框内输入full 点击下面的“Capture full size screenshot”就可以保存整个网页为图片了 原文出处 ...
- LeetCode 142:环形链表 II Linked List Cycle II
给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 - ...
- python学习笔记 - socket通信
socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...
- TEXT_CONVERT_XLS_TO_SAP 错误排查
转自:https://blog.csdn.net/ityangjia/article/details/88827308 本文链接:https://blog.csdn.net/ityangjia/art ...
- Enum.GetValues(),返回System.Array的一个实例
Array enumData = Enum.GetValues(e.GetType()); Console.WriteLine("This enum has {0} members.&quo ...
- 嵌入式Linux+NetCore 笔记一
记录嵌入式Linux+NetCore培训中遇到的一些问题以及解决方法 十一放假期间发现园里大神大石头(NewLife团队)开了一个嵌入式Linux+NetCore培训,就报名参加了.更幸运的是,我刚好 ...