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

参考答案

 1 class Solution {
 2 public:
 3     vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
 4
 5         vector<string>  res;
 6         unordered_map<string, int> map;
 7         for(size_t i = 0 ; i< list1.size(); i++)
 8             map[list1[i]] = i ; // list1 = key ; i = value ;
 9
10         size_t  min = INT_MAX;
11
12         for(size_t  i = 0 ; i<list2.size();i ++){
13             auto iter = map.find(list2[i]);
14             if(iter != map.end()){
15                 if(iter->second + i< min ){
16                     min = map[list2[i]] + i;
17                     res.assign(1,list2[i]); // 直接把值赋给第一个,不需要clear然后重新赋值
18
19                 }else if(iter->second + i  == min){
20                     res.push_back(list2[i]);
21                 }
22
23             }
24         }
25         return res;
26     }
27 };

分析备注

1.  for 循环中,int 替换为 size_t 可以加速。

2.  vector.assign(1,value) 可以实现 清除+赋值 两步操作。

3.  使用 iter -> first 和 iter -> second 可以更快。

原文地址:https://www.cnblogs.com/kykai/p/11614338.html

时间: 2024-09-29 15:57:24

LC 599. Minimum Index Sum of Two Lists的相关文章

【Leetcode_easy】599. Minimum Index Sum of Two Lists

problem 599. Minimum Index Sum of Two Lists 参考 1. Leetcode_easy_599. Minimum Index Sum of Two Lists; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11059712.html

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 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 betw

599. Minimum Index Sum of Two Lists

Problem statement: 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 i

[LeetCode] 599. Minimum Index Sum of Two Lists

https://leetcode.com/problems/minimum-index-sum-of-two-lists/ public class Solution { public String[] findRestaurant(String[] list1, String[] list2) { int leastSum = Integer.MAX_VALUE; List<String> result = new ArrayList<>(); Map<String, In

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 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 betw

LeetCode Minimum Index Sum of Two Lists

原题链接在这里:https://leetcode.com/problems/minimum-index-sum-of-two-lists/description/ 题目: 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

[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 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 betw

[LC] 64. Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. Example: Input: [   [1,3,1], [1,5

[leetcode]Minimum Path Sum @ Python

原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or r