★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10450571.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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.

假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。

你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅。 如果答案不止一个,则输出所有答案并且不考虑顺序。 你可以假设总是存在一个答案。

示例 1:

输入:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
输出: ["Shogun"]
解释: 他们唯一共同喜爱的餐厅是“Shogun”。

示例 2:

输入:
["Shogun", "Tapioca Express", "Burger King", "KFC"]
["KFC", "Shogun", "Burger King"]
输出: ["Shogun"]
解释: 他们共同喜爱且具有最小索引和的餐厅是“Shogun”,它有最小的索引和1(0+1)。

提示:

  1. 两个列表的长度范围都在 [1, 1000]内。
  2. 两个列表中的字符串的长度将在[1,30]的范围内。
  3. 下标从0开始,到列表的长度减1。
  4. 两个列表都没有重复的元素。

Runtime: 408 ms
Memory Usage: 20.4 MB
 1 class Solution {
 2     func findRestaurant(_ list1: [String], _ list2: [String]) -> [String] {
 3     var dict = [String: Int].init(minimumCapacity: list1.count)
 4     var dict1 = [String: Int].init(minimumCapacity: max(list1.count, list2.count))
 5
 6
 7     for (index,s) in list1.enumerated() {
 8         dict[s] = index
 9     }
10     var minIndex = list2.count + list1.count
11     for (index,s) in list2.enumerated() {
12         if let firstIndex = dict[s] {
13             let sum = firstIndex + index
14             minIndex = min(minIndex, sum)
15             dict1[s] = sum
16         }
17     }
18     var array = [String]()
19     for (key,value) in dict1 {
20         if value == minIndex {
21             array.append(key)
22         }
23     }
24     return array
25     }
26 }


476ms

 1 class Solution {
 2     func findRestaurant(_ list1: [String], _ list2: [String]) -> [String] {
 3         var mapping = [String: Int]()
 4         for i in 0..<list1.count {
 5             mapping[list1[i]] = i
 6         }
 7
 8         var ret = [String]()
 9         var indexSum = Int.max
10         for j in 0..<list2.count {
11             if let i = mapping[list2[j]] {
12                 let s = i + j
13                 if s > indexSum { continue }
14                 if s < indexSum {
15                     indexSum = s
16                     ret.removeAll()
17                 }
18                 ret.append(list2[j])
19             }
20         }
21         return ret
22     }
23 }


480ms

 1 class Solution {
 2     func findRestaurant(_ list1: [String], _ list2: [String]) -> [String] {
 3         var difference = Int.max
 4         var result = [String]()
 5         var dict1 = [String: Int]()
 6         for i in 0..<list1.count {
 7             dict1[list1[i]] = i
 8         }
 9         for i in 0..<list2.count {
10             if let j = dict1[list2[i]] {
11                 if i+j < difference {
12                     difference = i+j
13                     result = [list1[j]]
14                 } else if i+j == difference {
15                     result += [list1[j]]
16                 }
17             }
18         }
19         return result
20     }
21 }


504ms

 1 class Solution {
 2     func findRestaurant(_ list1: [String], _ list2: [String]) -> [String] {
 3         var dict = [String: Int]()
 4         var bucket = [[String]](repeating: [String](), count: list1.count + list2.count)
 5         for (idx, rest) in list1.enumerated() {
 6             dict[rest] = (dict[rest] ?? 0) + idx
 7         }
 8         for (idx, rest) in list2.enumerated() {
 9             if let s = dict[rest] {
10                 bucket[s+idx].append(rest)
11             }
12         }
13         for b in bucket {
14             if !b.isEmpty {
15                 return b
16             }
17         }
18         return [String]()
19     }
20 }


512ms

 1 class Solution {
 2     func findRestaurant(_ list1: [String], _ list2: [String]) -> [String] {
 3         let A = list1.count
 4         let B = list2.count
 5         guard A > 0, B > 0 else{return []}
 6         var maxSum = A+B
 7         var hash = [String:Int]()
 8         var resultSet = Set<String>()
 9         for i in 0..<A{
10             hash[list1[i]] = i
11         }
12
13         for i in 0..<B{
14             if hash[list2[i]] != nil{
15                 let sum = hash[list2[i]]! + i
16                 if sum < maxSum{
17                     resultSet.removeAll()
18                     resultSet.insert(list2[i])
19                     maxSum = sum
20                 }else if maxSum == sum{
21                     resultSet.insert(list2[i])
22                 }
23             }
24         }
25
26         return Array(resultSet)
27     }
28 }


520ms

 1 class Solution {
 2     func findRestaurant(_ list1: [String], _ list2: [String]) -> [String] {
 3         let set1 = Set<String>(list1)
 4         let set2 = Set<String>(list2)
 5         let dst = set1.intersection(set2)
 6
 7         var map1 = [String: Int]()
 8         for i in 0..<list1.count {
 9             if dst.contains(list1[i]) {
10                 map1[list1[i]] = i
11             }
12         }
13
14         var map2 = [String: Int]()
15         for i in 0..<list2.count {
16             if dst.contains(list2[i]) {
17                 map2[list2[i]] = i
18             }
19         }
20
21         var ans = [String]()
22         var minIndex = Int.max
23         dst.forEach { (c) in
24             let curIndex = map1[c]! + map2[c]!
25             if curIndex < minIndex {
26                 minIndex = curIndex
27             }
28         }
29
30         dst.forEach { (c) in
31             let curIndex = map1[c]! + map2[c]!
32             if curIndex == minIndex {
33                 ans.append(c)
34             }
35         }
36
37         return ans
38     }
39 }


556ms

 1 class Solution {
 2     func findRestaurant(_ list1: [String], _ list2: [String]) -> [String] {
 3     var dict = [String: Int].init(minimumCapacity: list1.count)
 4     var dict1 = [String: Int].init(minimumCapacity: max(list1.count, list2.count))
 5
 6
 7     for (index,s) in list1.enumerated() {
 8         dict[s] = index
 9     }
10     var minIndex = list2.count + list1.count
11     for (index,s) in list2.enumerated() {
12         if let firstIndex = dict[s] {
13             let sum = firstIndex + index
14             minIndex = min(minIndex, sum)
15             dict1[s] = sum
16         }
17     }
18     var array = [String]()
19     for (key,value) in dict1 {
20         if value == minIndex {
21             array.append(key)
22         }
23     }
24      return array
25     }
26 }


560ms

 1 class Solution {
 2     func findRestaurant(_ list1: [String], _ list2: [String]) -> [String] {
 3         var restMap: Dictionary<String, Int> = [:]
 4         for (index, rest) in list1.enumerated() {
 5             restMap[rest] = index
 6         }
 7
 8         var minRest: [String] = []
 9         for (index, rest) in list2.enumerated() {
10             guard let currIndex = restMap[rest] else {
11                 continue
12             }
13             let listIndex = currIndex + index
14             let minListIndex = (minRest.count > 0) ? restMap[minRest[0]] ?? Int.max : Int.max
15             if listIndex == minListIndex {
16                 minRest.append(rest)
17             } else if listIndex < minListIndex {
18                 minRest = [rest]
19             }
20             restMap[rest] = listIndex
21         }
22
23         return minRest
24     }
25 }


592ms

 1 class Solution {
 2     func findRestaurant(_ list1: [String], _ list2: [String]) -> [String] {
 3         var map1: [String: Int] = [:]
 4         for (i, s) in list1.enumerated() {
 5             map1[s] = i
 6         }
 7
 8         var res: [String] = []
 9         var mn = Int.max
10         for (i, s) in list2.enumerated() {
11             if let value = map1[s] {
12                 if value + i < mn {
13                     res = [s]
14                     mn = value + i
15                 }
16                 else if value + i == mn {
17                     res.append(s)
18                 }
19             }
20         }
21
22         return res
23     }
24 }


628ms

 1 class Solution {
 2     func findRestaurant(_ list1: [String], _ list2: [String]) -> [String] {
 3         var common = Set(list1).intersection(list2)
 4         let dic1 = Dictionary(uniqueKeysWithValues: zip(list1, 0..<list1.count))
 5         let dic2 = Dictionary(uniqueKeysWithValues: zip(list2, 0..<list2.count))
 6         var sum = Int.max
 7         var ans = [Int]()
 8
 9         for rt in common {
10             let i = dic1[rt]!
11             let j = dic2[rt]!
12             if (i+j < sum) {
13                 ans = [i]
14                 sum = i+j
15             } else if (i+j == sum) {
16                 ans.append(i)
17             }
18         }
19
20         return ans.compactMap{list1[$0]}
21     }
22 }


628ms

 1 class Solution {
 2     func findRestaurant(_ list1: [String], _ list2: [String]) -> [String] {
 3         guard list1.count > 0 && list2.count > 0 else {
 4             return []
 5         }
 6
 7         var restaurantDict: [String: Int] = [:]
 8         var sumDict: [Int: [String]] = [:]
 9
10         for (i, name) in list1.enumerated() {
11             restaurantDict[name] = i
12         }
13
14         for (i, name) in list2.enumerated() {
15             if let sum = restaurantDict[name] {
16                 if sumDict[sum + i] != nil {
17                     sumDict[sum + i]!.append(name)
18                 } else {
19                     sumDict[sum + i] = [name]
20                 }
21             }
22         }
23
24         let key = sumDict.keys.sorted().first ?? 0
25
26         return sumDict[key] ?? []
27     }
28 }


19676 kb

 1 class Solution {
 2     func findRestaurant(_ list1: [String], _ list2: [String]) -> [String] {
 3         var result = [String]()
 4         var indexVal = Int.max
 5
 6         for i in 0..<list1.count {
 7             for j in 0..<list2.count {
 8                 let indexOne = list1.index(list1.startIndex, offsetBy: i)
 9                 let indexTwo = list2.index(list2.startIndex, offsetBy: j)
10                 if list1[indexOne] == list2[indexTwo] {
11                     if (i + j) < indexVal {
12                         result = []
13                         indexVal = (i + j)
14                         result.append(list1[indexOne])
15                     }
16                     else if (i + j) == indexVal {
17                         result.append(list1[indexOne])
18                     }
19                 }
20             }
21         }
22         return result
23     }
24 }

转载于:https://www.cnblogs.com/strengthen/p/10450571.html

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

  1. leetcode 599. 两个列表的最小索引总和(Minimum Index Sum of Two Lists)

    目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的 ...

  2. 【每日一算法】两个列表的最小索引总和

    微信改版,加星标不迷路! 每日一算法-两个列表的最小索引总和 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的 ...

  3. 【LeetCode】第599题——两个列表的最小索引总和(难度:简单)

    [LeetCode]第599题--两个列表的最小索引总和(难度:简单) 题目描述 解题思路 代码详解 注意点 题目描述 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的 ...

  4. 599.两个列表的最小索引总和

    599.两个列表的最小索引总和 题目描述 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜 ...

  5. 599. 两个列表的最小索引总和【C++】

    题目地址: 599. 两个列表的最小索引总和 解题代码: class Solution { public:vector<string> findRestaurant(vector<s ...

  6. LeetCode简单题之两个列表的最小索引总和

    题目 假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答案不止一个 ...

  7. LeetCode 599. 两个列表的最小索引总和(哈希map)

    1. 题目 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答案不止一个, ...

  8. 【leetcode 简单】 第一百五十题 两个列表的最小索引总和

    假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答案不止一个,则输出所有答 ...

  9. 算法笔记(599. 两个列表的最小索引总和)

    题目: 假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答案不止一 ...

最新文章

  1. kali linux安装wine32,永恒之蓝msf下 ms17_010 (64位kali下安装wine32)
  2. 一文详解CSS常见的五大布局
  3. 无需格式转换直接发布DWG图纸到Autodesk Infrastructure Map Server(AIMS) 2013
  4. python的多线程适合计算密集操作_Python 多线程操作学习
  5. TrueCrypt加密:TrueCrypt Mount加载加密卷(2)
  6. PAT 乙级 1048 数字加密 (20 分)
  7. 基于STM32单片机电阻电容电感检测仪设计
  8. ACM的奇计淫巧系列
  9. Less语法-01-简介
  10. 美联储加息负面效应外溢
  11. ubuntu下查看软件安装信息
  12. 计算机无法与internet同步时间,win7系统能上网可是无法同步Internet时间的解决方法...
  13. 去中心化自治组织DAO——Steemit社区介绍
  14. 第4章 设计目标与原则
  15. 分布式系统的解决方案,学好这个就够了
  16. tensorflow2: attention机制实现
  17. QML---Repeater
  18. 新生搜索神器Microsoft Academic Search与Google scholar、PubMed、wos、embase大PK!
  19. 电子科技大学2021计算机考研复试科目,2021电子科技大学考研大纲参考书目
  20. Proxmox VE(PVE) 安装 网心云

热门文章

  1. Git提交本地代码到GitHub
  2. hadoop学习——Hadoop核心组件
  3. URL重写(使用微软URLRewriter)
  4. C#完整的通信代码(点对点,点对多,同步,异步,UDP,TCP),多多宜善
  5. c语言数据结构五子棋实验报告,数据结构课程设计-五子棋
  6. uc3842改可调电源教程_36W LED 防水电源
  7. Markdown--Latex公式编辑_验证
  8. Unity Text 插入超链接
  9. 设计模式学习笔记——迭代器(Iterator)模式
  10. 3D 机器视觉 02 - FPGA生成N位元格雷码