SoFunction
Updated on 2025-03-01

Go language problem solving LeetCode599's minimum index sum of two lists

Question description

599. The minimum index sum of two lists

AssumptionsAndyandDorisWant to choose a restaurant for dinner, and they all have a list of favorite restaurants, each with a string of names.

You need to help them with minimal indexing and find out the restaurant they share. If more than one answer is required, all answers are output and the order is not considered. You can assume that the answer always exists.

Example 1:

enter: list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"],list2 = ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
Output: ["Shogun"]
explain: The only restaurant they love together is“Shogun”。

Example 2:

enter:list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"],list2 = ["KFC", "Shogun", "Burger King"]
Output: ["Shogun"]
explain: The restaurant they share and has the smallest index and is“Shogun”,It has the smallest index and1(0+1)。

hint:

  • 1 <= , <= 1000
  • 1 <= list1[i].length, list2[i].length <= 30 
  • list1[i] and list2[i] are composed of space ' ' and English letters.
  • All strings of list1 are unique.
  • All strings in list2 are unique.

Idea Analysis

In fact, the meaning of the question is not to find whether there is something in common, but to find the one that is common and indexed and smallest. A different way of understanding is that two friends want to find a restaurant that they like. The restaurants they like are sorted by the distance from 2 people. I want to find a restaurant that is close to 2 people. This way it is easy to understand.

So the idea is:

The restaurant table of one person is existing, the key is the restaurant, the value is the distance

Go to the restaurant table that matches the second person, find out if it is the minimum distance. If it is yes, clear the previous one and add it to the list.

AC Code

class Solution {
    public String[] findRestaurant(String[] list1, String[] list2) {
            if(list1 == null||list2 == null){
                return null;
            }
            HashMap<String,Integer> bucket = new HashMap<>();
            for(int i = 0;i < ;i++){
                (list1[i],i);
            }
            List<String> res = new ArrayList<>();
            int minIndexSum = Integer.MAX_VALUE;
            for(int i = 0;i< ;i++){
                if((list2[i])){
                    if((list2[i])+i<minIndexSum){
                        minIndexSum = (list2[i])+i;
                        ();
                        (list2[i]);
                    }else if((list2[i])+i == minIndexSum){
                        (list2[i]);
                    }
                }
            }
            return (new String[()]);
    }
}

refer to

4 lines of code, simple idea, efficient performance - the minimum index sum of two lists - LeetCode ()

The above is the detailed content of the minimum index sum of the two lists of LeetCode599 in the Go language problem solution. For more information on the minimum index sum of the two lists of Go problems, please pay attention to my other related articles!