599. Minimum index sum of two lists

Posted by cuteflower on Sat, 05 Mar 2022 07:11:10 +0100


Force bucklehttps://leetcode-cn.com/problems/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. The name of each restaurant is represented by a string.

You need to help them use the least index and find their favorite restaurants. If there is more than one answer, all answers are output regardless of order. You can assume that the answer always exists.

Example 1:

Input: list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"], List2 = ["Piatti", "the grill at Torrey Pines", "Hungary Hunter Steakhouse", "Shogun"]
Output: [Shogun]
Explanation: their only favorite restaurant is "Shogun".

Example 2:

Input: list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"], list2 = ["KFC", "Shogun", "Burger King"]
Output: [Shogun]
Explanation: their favorite restaurant with the smallest index sum is "Shogun", which has the smallest index and 1 (0 + 1).


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

Source: LeetCode
Link: https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.


The last failed idea first:

I didn't pay attention to the qualification, thinking that I only need to return the found string.

Regardless of violence, the count is based on the length of the string (of course, there may be multiple or even all strings of the same length)

By building custom structures

typedef struct {
    char str[SSIZE];    // Store list string
    int size;           // String length

typedef struct{
    int capability;
    int cnt[CNT_SIZE];    // Count array, val = string length

In this way, by traversing as long as the size is equal, and then comparing, you can indeed find all the same strings, but the minimum idx is limited and there is no way to deal with it

New ideas:

Starting with the smallest idx, we check whether there is a match between each idx and, and then put in the result. As long as this idx is found, it can be returned, because the subsequent idx sum becomes larger.

For example:

1. list1 length = m, list2 length = n

2. The range of LIST1 [0, m-1], list2 [0, n-1], and idx is [0, m+n-2]

3. The sequence number i of LIST1 and the sequence number j of list2 = current sum - i. as long as j is a reasonable range of list2, we can perform string matching

 * Note: The returned array must be malloced, assume caller calls free().
char ** findRestaurant(char ** list1, int list1Size, char ** list2, int list2Size, int* returnSize){
    char **ret = (char **)malloc(sizeof(char *) * 1000);
    int cnt = 0;
    int maxIdxSum = list1Size+list2Size-2;
    for(int k=0; k<=maxIdxSum; k++)
        for(int i=0; i<list1Size; i++)
            int j = k-i;
            //printf("k=%d,i=%d,j=%d\n", k, i, j);
            if(j >= 0 && j<list2Size)
                //printf("list1[%d]=%s, list2[%d]=%s\n", i, list1[i], j, list2[j]);
                if(strcmp(list1[i], list2[j]) == 0)
                    //printf("find...%s, sum=%d\n", list1[i]);
                    ret[cnt] = (char *)malloc(sizeof(char) * 31);
                    strcpy(ret[cnt], list1[i]);
        if(cnt > 0)

    *returnSize = cnt;
    return ret;

View more brush notes

Topics: leetcode