Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
# [599.Minimum Index Sum of Two Lists][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Given two arrays of strings `list1` and `list2`, find the **common strings with the least index sum**.

A **common string** is a string that appeared in both `list1` and `list2`.

A **common string with the least index sum** is a common string such that if it appeared at `list1[i]` and `list2[j]` then `i + j` should be the minimum value among all the other common **strings**.

Return all the **common strings with the least index sum**. Return the answer in **any order**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
Output: ["Shogun"]
Explanation: The only common string is "Shogun".
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Minimum Index Sum of Two Lists
```go
```
Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"]
Output: ["Shogun"]
Explanation: The common string with the least index sum is "Shogun" with index sum = (0 + 1) = 1.
```

**Example 3:**

```
Input: list1 = ["happy","sad","good"], list2 = ["sad","happy","good"]
Output: ["sad","happy"]
Explanation: There are three common strings:
"happy" with index sum = (0 + 1) = 1.
"sad" with index sum = (1 + 0) = 1.
"good" with index sum = (2 + 2) = 4.
The strings with the least index sum are "sad" and "happy".
```

## 结语

Expand Down
24 changes: 22 additions & 2 deletions leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(list1 []string, list2 []string) []string {
map2 := make(map[string]int)
for index, str := range list2 {
map2[str] = index
}
ret := make([]string, 0)
sum := 0
minSum := 2000
for index, str := range list1 {
if i, ok := map2[str]; ok {
sum = index + i
if sum == minSum {
ret = append(ret, str)
continue
}
if sum < minSum {
ret = []string{str}
minSum = sum
}
}
}
return ret
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
list1, list2 []string
expect []string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []string{"Shogun", "Tapioca Express", "Burger King", "KFC"}, []string{"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"}, []string{"Shogun"}},
{"TestCase2", []string{"Shogun", "Tapioca Express", "Burger King", "KFC"}, []string{"KFC", "Shogun", "Burger King"}, []string{"Shogun"}},
{"TestCase3", []string{"happy", "sad", "good"}, []string{"sad", "happy", "good"}, []string{"happy", "sad"}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.list1, c.list2)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.list1, c.list2)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading