原题链接在这里: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. Paper | Deep Residual Learning for Image Recognition

    目录 1. 故事 2. 残差学习网络 2.1 残差块 2.2 ResNet 2.3 细节 3. 实验 3.1 短连接网络与plain网络 3.2 Projection解决短连接维度不匹配问题 3.3 ...

  2. Springboot使用ehcache缓存

    本文部分步骤继承于springboot使用cache缓存,如果有不清楚的,请移驾springboot使用cache缓存 ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存,Java ...

  3. mysql 创建用户, 分配权限, 删除用户

    通过create user 命令来创建用户, 有两种方式:(只介绍通过 create user 命令, 直接往user表中插入数据的方式,这里就不说了) 创建用户的同时, 指定用户可登录的主机和密码 ...

  4. 转 推荐 33 个 IDEA 最牛配置,写代码太爽了!

    本文由 简悦 SimpRead 转码, 原文地址 https://mp.weixin.qq.com/s/neyvJouuG1Rmxn3BwfRXVg 作者:琦彦 blog.csdn.net/fly91 ...

  5. Spring Cloud Eureka 服务注册中心(二)

    序言 Eureka 是 Netflix 开发的,一个基于 REST 服务的,服务注册与发现的组件 它主要包括两个组件:Eureka Server 和 Eureka Client Eureka Clie ...

  6. RSyslog Windows Agent 安装配置

    下载地址:https://www.rsyslog.com/windows-agent/windows-agent-download/ 安装过程: 1.双击rsyslogwa安装包,开始进行安装 2.一 ...

  7. 【mysql】Mysql的profile的使用 --- Profilling mysql的性能分析工具

    分析SQL执行带来的开销是优化SQL的重要手段. 在MySQL数据库中,可以通过配置profiling参数来启用SQL剖析.该参数可以在全局和session级别来设置.对于全局级别则作用于整个MySQ ...

  8. HTML教程详解

    HTML学习笔记 目录 一.html简介 1.html是什么? 2.html能做什么(html的作用)? 3.html书写规范 二.html基本标签 1.标签的语法 2.标签的分类 3.常用标签: 1 ...

  9. dedecms5.7的获取本文章的TAG

    tag调用标签如下: {dede:tag row='10' getall='1' sort='month'} <li><a href='[field:link/]'>[fiel ...

  10. Caused by: java.lang.ClassNotFoundException: org.springframework.data.repository.config.BootstrapMode

    1.起因,启动SpringBoot2.0的时候报了这个错误.说找不到类,咱也是刚学SpringBoot2.0,咱也不懂,咱也不知道问谁,研究一翻,找不到原因就百度了. 参考链接:https://blo ...