diff --git a/leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/README.md b/leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/README.md index 677f61a3f..fb36732de 100755 --- a/leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/README.md +++ b/leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/README.md @@ -1,28 +1,40 @@ # [1855.Maximum Distance Between a Pair of Values][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 +You are given two **non-increasing 0-indexed** integer arrays `nums1` and `nums2`. + +A pair of indices (`i, j`), where `0 <= i < nums1.length` and `0 <= j < nums2.length`, is **valid** if both `i <= j` and `nums1[i] <= nums2[j]`. The **distance** of the pair is `j - i`. + +Return the **maximum distance** of any **valid** pair (`i, j`). If there are no valid pairs, return `0`. + +An array `arr` is **non-increasing** if `arr[i-1] >= arr[i]` for every `1 <= i < arr.length`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5] +Output: 2 +Explanation: The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4). +The maximum distance is 2 with pair (2,4). ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Maximum Distance Between a Pair of Values -```go ``` +Input: nums1 = [2,2,2], nums2 = [10,10,1] +Output: 1 +Explanation: The valid pairs are (0,0), (0,1), and (1,1). +The maximum distance is 1 with pair (0,1). +``` + +**Example 3:** +``` +Input: nums1 = [30,29,19,5], nums2 = [25,25,25,25,25] +Output: 2 +Explanation: The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4). +The maximum distance is 2 with pair (2,4). +``` ## 结语 diff --git a/leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/Solution.go b/leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/Solution.go index d115ccf5e..c99aa707f 100644 --- a/leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/Solution.go +++ b/leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/Solution.go @@ -1,5 +1,16 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums1 []int, nums2 []int) int { + var ret int + l := len(nums2) + index := 0 + for idx := range nums1 { + for ; index < l && nums2[index] >= nums1[idx]; index++ { + } + ret = max(ret, index-1-idx) + } + if index == 0 { + return 0 + } + return ret } diff --git a/leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/Solution_test.go b/leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/Solution_test.go index 14ff50eb4..382fd1dcb 100644 --- a/leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/Solution_test.go +++ b/leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + nums1, nums2 []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{55, 30, 5, 4, 2}, []int{100, 20, 10, 10, 5}, 2}, + {"TestCase2", []int{2, 2, 2}, []int{10, 10, 1}, 1}, + {"TestCase3", []int{30, 29, 19, 5}, []int{25, 25, 25, 25, 25}, 2}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums1, c.nums2) 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.nums1, c.nums2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }