Skip to content

Commit 4a69ba3

Browse files
authored
Merge pull request #1445 from 0xff-dev/1855
Add solution and test-cases for problem 1855
2 parents f748f48 + cad5977 commit 4a69ba3

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
# [1855.Maximum Distance Between a Pair of Values][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given two **non-increasing 0-indexed** integer arrays `nums1` and `nums2`.
5+
6+
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`.
7+
8+
Return the **maximum distance** of any **valid** pair (`i, j`). If there are no valid pairs, return `0`.
9+
10+
An array `arr` is **non-increasing** if `arr[i-1] >= arr[i]` for every `1 <= i < arr.length`.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]
16+
Output: 2
17+
Explanation: The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4).
18+
The maximum distance is 2 with pair (2,4).
1319
```
1420

15-
## 题意
16-
> ...
17-
18-
## 题解
21+
**Example 2:**
1922

20-
### 思路1
21-
> ...
22-
Maximum Distance Between a Pair of Values
23-
```go
2423
```
24+
Input: nums1 = [2,2,2], nums2 = [10,10,1]
25+
Output: 1
26+
Explanation: The valid pairs are (0,0), (0,1), and (1,1).
27+
The maximum distance is 1 with pair (0,1).
28+
```
29+
30+
**Example 3:**
2531

32+
```
33+
Input: nums1 = [30,29,19,5], nums2 = [25,25,25,25,25]
34+
Output: 2
35+
Explanation: The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4).
36+
The maximum distance is 2 with pair (2,4).
37+
```
2638

2739
## 结语
2840

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums1 []int, nums2 []int) int {
4+
var ret int
5+
l := len(nums2)
6+
index := 0
7+
for idx := range nums1 {
8+
for ; index < l && nums2[index] >= nums1[idx]; index++ {
9+
}
10+
ret = max(ret, index-1-idx)
11+
}
12+
if index == 0 {
13+
return 0
14+
}
15+
return ret
516
}

leetcode/1801-1900/1855.Maximum-Distance-Between-a-Pair-of-Values/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
nums1, nums2 []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{55, 30, 5, 4, 2}, []int{100, 20, 10, 10, 5}, 2},
17+
{"TestCase2", []int{2, 2, 2}, []int{10, 10, 1}, 1},
18+
{"TestCase3", []int{30, 29, 19, 5}, []int{25, 25, 25, 25, 25}, 2},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums1, c.nums2)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.nums1, c.nums2)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)