-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1855-MaximumDistanceBetweenAPairOfValues.go
More file actions
100 lines (86 loc) · 3.8 KB
/
1855-MaximumDistanceBetweenAPairOfValues.go
File metadata and controls
100 lines (86 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
// 1855. Maximum Distance Between a Pair of Values
// 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: 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:
// 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).
// Constraints:
// 1 <= nums1.length, nums2.length <= 10^5
// 1 <= nums1[i], nums2[j] <= 10^5
// Both nums1 and nums2 are non-increasing.
import "fmt"
func maxDistance(nums1 []int, nums2 []int) int {
res := 0
for i := 0 ; i < len(nums1); i++ {
low, high := i, len(nums2) - 1
for low <= high {
mid := (low + high) / 2
if nums2[mid] >= nums1[i] {
if mid - i > res { res = mid - i }
low = mid + 1
} else {
high = mid - 1
}
}
}
return res
}
func maxDistance1(nums1 []int, nums2 []int) int {
res := 0
max := func (x, y int) int { if x > y { return x; }; return y; }
for i, j := 0, 0; j < len(nums2); j++ {
for i < len(nums1) && nums1[i] > nums2[j] { i++ }
if i == len(nums1) { break }
res = max(res, j - i)
}
return res
}
func main() {
// Example 1:
// 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).
fmt.Println(maxDistance([]int{55,30,5,4,2}, []int{100,20,10,10,5})) // 2
// Example 2:
// 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).
fmt.Println(maxDistance([]int{2,2,2}, []int{10,10,1})) // 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).
fmt.Println(maxDistance([]int{30,29,19,5}, []int{25,25,25,25,25})) // 2
fmt.Println(maxDistance([]int{1,2,3,4,5,6,7,8,9}, []int{1,2,3,4,5,6,7,8,9})) // 8
fmt.Println(maxDistance([]int{1,2,3,4,5,6,7,8,9}, []int{9,8,7,6,5,4,3,2,1})) // 8
fmt.Println(maxDistance([]int{9,8,7,6,5,4,3,2,1}, []int{1,2,3,4,5,6,7,8,9})) // 0
fmt.Println(maxDistance([]int{9,8,7,6,5,4,3,2,1}, []int{9,8,7,6,5,4,3,2,1})) // 0
fmt.Println(maxDistance1([]int{55,30,5,4,2}, []int{100,20,10,10,5})) // 2
fmt.Println(maxDistance1([]int{2,2,2}, []int{10,10,1})) // 1
fmt.Println(maxDistance1([]int{30,29,19,5}, []int{25,25,25,25,25})) // 2
fmt.Println(maxDistance1([]int{1,2,3,4,5,6,7,8,9}, []int{1,2,3,4,5,6,7,8,9})) // 8
fmt.Println(maxDistance1([]int{1,2,3,4,5,6,7,8,9}, []int{9,8,7,6,5,4,3,2,1})) // 8
fmt.Println(maxDistance1([]int{9,8,7,6,5,4,3,2,1}, []int{1,2,3,4,5,6,7,8,9})) // 0
fmt.Println(maxDistance1([]int{9,8,7,6,5,4,3,2,1}, []int{9,8,7,6,5,4,3,2,1})) // 0
}