diff --git a/leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/README.md b/leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/README.md index 62a590e6b..b957ce51e 100644 --- a/leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/README.md +++ b/leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/README.md @@ -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". +``` ## 结语 diff --git a/leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/Solution.go b/leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/Solution.go index d115ccf5e..f01e87a1f 100644 --- a/leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/Solution.go +++ b/leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/Solution.go @@ -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 } diff --git a/leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/Solution_test.go b/leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/Solution_test.go index 14ff50eb4..0a7b99152 100644 --- a/leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/Solution_test.go +++ b/leetcode/501-600/0599.Minimum-Index-Sum-of-Two-Lists/Solution_test.go @@ -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() { }