09 Sept 2026Go / Python / TypeScriptEasy

Minimum Index Sum of Two Lists

Return common restaurants whose index sum across two preference lists is minimal.

Index the first list in a map, scan the second list, and keep only matches tied for the smallest combined index.

complexity

O(n + m) time and O(n) space for list lengths n and m.

solution files

  • Go minimum-index-sum-of-two-lists/solution.go
  • Python minimum-index-sum-of-two-lists/solution.py
  • TypeScript minimum-index-sum-of-two-lists/solution.ts

Solution files

Gominimum-index-sum-of-two-lists/solution.go
package main

func findRestaurant(list1 []string, list2 []string) []string {
	firstIndex := make(map[string]int, len(list1))
	for index, name := range list1 {
		firstIndex[name] = index
	}
	result := []string{}
	best := int(^uint(0) >> 1)
	for index, name := range list2 {
		other, ok := firstIndex[name]
		if !ok {
			continue
		}
		sum := index + other
		if sum < best {
			best, result = sum, []string{name}
		} else if sum == best {
			result = append(result, name)
		}
	}
	return result
}
Pythonminimum-index-sum-of-two-lists/solution.py
class Solution:
    def findRestaurant(self, list1: list[str], list2: list[str]) -> list[str]:
        first_index = {name: index for index, name in enumerate(list1)}
        result: list[str] = []
        best = float(class="syntax-string">"inf")
        for index, name in enumerate(list2):
            if name not in first_index:
                continue
            index_sum = index + first_index[name]
            if index_sum < best:
                best, result = index_sum, [name]
            elif index_sum == best:
                result.append(name)
        return result
TypeScriptminimum-index-sum-of-two-lists/solution.ts
function findRestaurant(list1: string[], list2: string[]): string[] {
  const firstIndex = new Map(list1.map((name, index) => [name, index]));
  const result: string[] = [];
  let best = Number.POSITIVE_INFINITY;
  for (let index = class="syntax-number">0; index < list2.length; index += class="syntax-number">1) {
    const other = firstIndex.get(list2[index]);
    if (other === undefined) continue;
    const sum = index + other;
    if (sum < best) { best = sum; result.length = class="syntax-number">0; result.push(list2[index]); }
    else if (sum === best) result.push(list2[index]);
  }
  return result;
}