Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.

You need to help them find out their common interest with the least list index sum. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.

Example 1:

Input:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
Output: ["Shogun"]
Explanation: The only restaurant they both like is "Shogun".

Example 2:

Input:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["KFC", "Shogun", "Burger King"]
Output: ["Shogun"]
Explanation: The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1).

Note:

  1. The length of both lists will be in the range of [1, 1000].
  2. The length of strings in both lists will be in the range of [1, 30].
  3. The index is starting from 0 to the list length minus 1.
  4. No duplicates in both lists.

这道题给了我们两个字符串数组,让我们找到坐标位置之和最小的相同的字符串。那么对于这种数组项和其坐标之间关系的题,最先考虑到的就是要建立数据和其位置坐标之间的映射。我们建立list1的值和坐标的之间的映射,然后遍历list2,如果当前遍历到的字符串在list1中也出现了,那么我们计算两个的坐标之和,如果跟我们维护的最小坐标和mn相同,那么将这个字符串加入结果res中,如果比mn小,那么mn更新为这个较小值,然后将结果res清空并加入这个字符串,参见代码如下:

class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
vector<string> res;
unordered_map<string, int> m;
int mn = INT_MAX, n1 = list1.size(), n2 = list2.size();
for (int i = ; i < n1; ++i) m[list1[i]] = i;
for (int i = ; i < n2; ++i) {
if (m.count(list2[i])) {
int sum = i + m[list2[i]];
if (sum == mn) res.push_back(list2[i]);
else if (sum < mn) {
mn = sum;
res = {list2[i]};
}
}
}
return res;
}
};

类似题目:

Intersection of Two Linked Lists

参考资料:

https://discuss.leetcode.com/topic/90534/java-o-n-m-time-o-n-space

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Minimum Index Sum of Two Lists 两个表单的最小坐标和的更多相关文章

  1. LeetCode Minimum Index Sum of Two Lists

    原题链接在这里:https://leetcode.com/problems/minimum-index-sum-of-two-lists/description/ 题目: Suppose Andy a ...

  2. 599. Minimum Index Sum of Two Lists两个餐厅列表的索引和最小

    [抄题]: Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of fa ...

  3. 【Leetcode_easy】599. Minimum Index Sum of Two Lists

    problem 599. Minimum Index Sum of Two Lists 题意:给出两个字符串数组,找到坐标位置之和最小的相同的字符串. 计算两个的坐标之和,如果与最小坐标和sum相同, ...

  4. LeetCode 599. Minimum Index Sum of Two Lists (从两个lists里找到相同的并且位置总和最靠前的)

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  5. LeetCode 599: 两个列表的最小索引总和 Minimum Index Sum of Two Lists

    题目: 假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. Suppose Andy and Doris want to cho ...

  6. [Swift]LeetCode599. 两个列表的最小索引总和 | Minimum Index Sum of Two Lists

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  7. 【LeetCode】599. Minimum Index Sum of Two Lists 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:找到公共元素再求索引和 方法二:索引求和,使 ...

  8. C#LeetCode刷题之#599-两个列表的最小索引总和​​​​​​​​​​​​​​(Minimum Index Sum of Two Lists)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3802 访问. 假设Andy和Doris想在晚餐时选择一家餐厅,并 ...

  9. [LeetCode&Python] Problem 599. Minimum Index Sum of Two Lists

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

随机推荐

  1. [jdoj1817]Drainage Ditches_网络流

    Drainage Ditches jdoj-1817 题目大意:网络流裸求最大流 注释:n(点数),m(边数)<=200. 想法:裸的网络流求最大流,用bfs+dfs,美其名曰dinic. 没有 ...

  2. sqlite的limit使用

    如果我要取11-20的Account表的数据,则为: Select * From Person  Limit 9 Offset 10;表示从Person  表获取数据,跳过10行,取9行 .也可以这样 ...

  3. 原生js封装添加class,删除class

    一.添加class function addClass(ele,cName) { var arr = ele.className.split(' ').concat(cName.split(" ...

  4. VC++开发AutoCAD 2018/objectARX 用向导新建项目无法新建的问题

    话说笔者最近想用新机子上装的AutoCAD ObjectARX 2018来进行二次开发,兴致勃勃安装了ARX API和向导, 然后打开VS2015,新建项目,无法新建. 折腾了一下,还是没有解决,后面 ...

  5. NO.7 项目需求分析

    NO.7 项目需求分析 由于我们组的第一次选题并没有通过,所以我们又重新选择了一个题目--高校学生征信系统. 结合老师的作业要求,我们对该项目进行了详细的需求分析,软件需求规格说明书地址请点击这里.软 ...

  6. 【基础知识】Flex-弹性布局原来如此简单!!

    简言 布局的传统解决方案是基于盒状模型,依赖 display + position + float 方式来实现,灵活性较差.2009年,W3C提出了一种新的方案-Flex,Flex是Flexible ...

  7. JAVA_SE基础——52.匿名内部类

    电信的电箱烧了,害我断了2天网,真拿命,耽误了 Java匿名内部类的总结: 没有名字的内部类.就是内部类的简化形式.一般只用一次就可以用这种形式.匿名内部类其实就是一个匿名子类对象.想要定义匿名内部类 ...

  8. 【深度学习】深入理解ReLU(Rectifie Linear Units)激活函数

    论文参考:Deep Sparse Rectifier Neural Networks (很有趣的一篇paper) Part 0:传统激活函数.脑神经元激活频率研究.稀疏激活性 0.1  一般激活函数有 ...

  9. Mego开发文档 - 复杂查询

    复杂查询 Mego 还支持一些更高级的LLINQ查询写法,本文只列出一部分. 分组汇总查询 using (var db = new OrderManageEntities()) { var query ...

  10. xftp上传文件失败,执行程序发现磁盘满了:No space left on device

    参考链接 No space left on device 解决Linux系统磁盘空间满的办法http://www.cnblogs.com/aspirant/p/3604801.html如何解决linu ...