[Swift]LeetCode599. 两个列表的最小索引总和 | Minimum Index Sum of Two Lists
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(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:
- The length of both lists will be in the range of [1, 1000].
- The length of strings in both lists will be in the range of [1, 30].
- The index is starting from 0 to the list length minus 1.
- 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, 1000]内。
- 两个列表中的字符串的长度将在[1,30]的范围内。
- 下标从0开始,到列表的长度减1。
- 两个列表都没有重复的元素。
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 }