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.

题目标签:Hash Table

  这道题让我们从两个list中找出相同的string并且它们的index sum是最小的,意思就是让我们找到他们两个都想吃的同一个饭店,并且这个饭店是他们排位里最靠前的。

Step 1: 首先我们建立一个HashMap, 遍历list1, 把饭店string作为key, 把index作为value保存进map。

Step 2: 建立一个HashSet, 遍历list2, 如果找到list2里的饭店string是在之前的map里的话,更新一下map的value = index1(之前的value) + index2(在list2里的);并且设一个min,在记录最小的index sum,把最小的饭店string保存进set里面。

    当找到一个相同的index sum = min的话,把这个饭店string加入set;当找到更小的min的时候,要把set清空,因为出现更小的index sum的饭店了,淘汰前面的饭店,重设min的值。

Step 3: 此时set里的饭店名就是最靠前的并且是两个list都有的,设置一个string array把答案copy进去return。

   

Java Solution:

Runtime beats 69.17%

完成日期:06/07/2017

 public class Solution
{
public String[] findRestaurant(String[] list1, String[] list2)
{
HashMap<String, Integer> map = new HashMap<>(); // put list1's string as key, index as value.
for(int i=0; i<list1.length; i++)
map.put(list1[i], i); HashSet<String> set = new HashSet<>(); int min = Integer.MAX_VALUE; // iterate list2 to see any string is in map
for(int i=0; i<list2.length; i++)
{
if(map.get(list2[i]) != null) // if list2's string is in map
{
int j = map.get(list2[i]);
map.put(list2[i], i + j); // update map's value if(i+j == min) // if find another same min value, add this string to set.
set.add(list2[i]);
else if(i+j < min) // if find another smaller index
{
set.clear(); // clear the set.
set.add(list2[i]); // add smaller index string into set.
min = i+j; // update the min;
} }
} String[] res = new String[set.size()]; int i=0;
for(String s : set)
{
res[i] = s;
i++;
} return res;
}
}

参考资料:

http://blog.csdn.net/kangbin825/article/details/72794253

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

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

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

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

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

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

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

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

  5. 599. Minimum Index Sum of Two Lists(easy)

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

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

  7. LC 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 fav ...

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

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

  9. [LeetCode] 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. 复用代码【SSH配置文件】

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...

  2. mysql水平分表和垂直分表的优缺点

    表分割有两种方式: 1.水平分割:根据一列或多列数据的值把数据行放到两个独立的表中. 水平分割通常在下面的情况下使用. •表很大,分割后可以降低在查询时需要读的数据和索引的页数,同时也降低了索引的层数 ...

  3. MyBatis框架(三)动态SQL,分页,二进制存入数据库图片

    一.动态sql语句,分页 1, <if>条件 <if test="key!=null"> 拼接sql语句 </if> 2, <choose ...

  4. JS设计模式(二) 惰性模式

    惰性模式:减少代码每次执行时的重复性判断,通过重新定义对象来避免原对象中的分支判断,提高网站性能. 例如针对不同浏览器的事件注册方法: var AddEvent = function(dom, typ ...

  5. Java Byte取值范围

    Java Byte 的取值范围大家都知道(-128 ~ 127),那么-128 和 127 这两个数是怎么计算的呢? #大学知识回顾: 概念:负数的补码是该 数 绝 对 值 的 原 码 按 位 取 反 ...

  6. 用es6的class关键字定义一个类

    es6新增class关键字使用方法详解. 通过class关键字,可以定义类.基本上,ES6的class可以看作只是一个语法糖,它的绝大部分功能,ES5都可以做到,新的class写法只是让对象原型的写法 ...

  7. 对Item中定时器的理解

    一.Diamond介绍 Diamond主要提供持久配置的发布和订阅服务,最大特点是结构简单,稳定可靠. 主要的使用场景:TDDL使用Diamond动态切换数据库,动态扩容等:业务使用Diamond推送 ...

  8. 一个“.java”文件中是否可以包含多个类(不是内部类)?有什么限制?

    可以,若这个类的修饰符是public则,其类名须与文件名相同.

  9. PHP 生成毫秒时间戳

    PHP的time()函数生成当前时间的秒数,但是在一些情况下我们需要获取当前服务器时间和GMT(格林威治时间)1970年1月0时0分0秒的毫秒数,与Java中的currentTimeMilis()函数 ...

  10. poj1014二进制优化多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53029   Accepted: 13506 Descri ...