原题链接在这里: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 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.

题解:

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的更多相关文章

  1. 【leetcode】1257. Smallest Common Region

    题目如下: You are given some lists of regions where the first region of each list includes all other reg ...

  2. Smallest Common Multiple-freecodecamp算法题目

    Smallest Common Multiple 1.要求 找出能被两个给定参数和它们之间的连续数字整除的最小公倍数. 2.思路 设定一个twoMultiple(a,b)函数,求出输入两个参数的最小公 ...

  3. [Intermediate Algorithm] - Smallest Common Multiple

    题目 找出能被两个给定参数和它们之间的连续数字整除的最小公倍数. 范围是两个数字构成的数组,两个数字不一定按数字顺序排序. 例如对 1 和 3 —— 找出能被 1 和 3 和它们之间所有数字整除的最小 ...

  4. [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 ...

  5. [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 ...

  6. 【leetcode&CN&竞赛】1198.Find Smallest Common Element in All Rows

    题目如下: 给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1. 示例: 输入:mat = [ ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Comet OJ CCPC-Wannafly & Comet OJ 夏季欢乐赛(2019)

    Preface 在一个月黑风高的夜晚我这个蒟蒻正踌躇着打什么比赛好 是继续做一场AGC,还是去刷一场CF 然后,一道金光闪过(滑稽),我们的红太阳bzt给我指明了方向: 你太菜了,我知道有一场很水的比 ...

  2. Istio Routing极简教程

    官网文档: https://istio.io/docs/reference/config/networking/#VirtualService 在学习像Istio这样的新技术时,看一下示例应用程序总是 ...

  3. golang基础之工程结构

    Golang 工作空间 编译工具对源码目录有严格要求,每个工作空间 (workspace) 必须由 bin.pkg.src 三个目录组成. workspace | +--- bin // go ins ...

  4. LeetCode 150:逆波兰表达式求值 Evaluate Reverse Polish Notation

    题目: 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. Evaluate the value of an arithm ...

  5. LeetCode 19:删除链表的倒数第N个节点 Remove Nth Node From End of List

    给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. Given a linked list, remove the n-th node from the end of list and ...

  6. 生成 RSA 公钥和私钥的方法

    在使用 RSA 加密算法时,需要使用到一对 公钥 和 私钥,生成 公钥 和 私钥 需要借助 openssl 这款工具,下载这款工具的地址如下: http://slproweb.com/products ...

  7. 记一次内存无法回收导致频繁fullgc机器假死的思路

    确定挂机 络绎不绝的来不同类型的bug 当bug滚滚而来时,不要怀疑,你的发布的应用基本是不可用状态了.观察哨兵监控数据,特别是内存打到80%基本就挂机了,或者监控数据缺失也基本是挂机了.此时应当马上 ...

  8. postgresql in 优化

    原sql: SELECT res_id_ori FROM wk_sheet A, wk_page b WHERE A .wk_sheet_id = b.wk_sheet_id ') ; 原sql执行计 ...

  9. C#,二分法,BinarySearch()

    static int BinarySearch(int[] arr,int key,int low,int high) { low = 0;high = arr.Length - 1; while(l ...

  10. C#编辑xml文件

    string path = @"C:\Users\Administrator\Desktop\无人智能便利店\install\收银端\XMLRFI.xml"; XmlDocumen ...