-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2613-BeautifulPairs.go
More file actions
98 lines (89 loc) · 3.81 KB
/
2613-BeautifulPairs.go
File metadata and controls
98 lines (89 loc) · 3.81 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
package main
// 2613. Beautiful Pairs
// You are given two 0-indexed integer arrays nums1 and nums2 of the same length.
// A pair of indices (i,j) is called beautiful if|nums1[i] - nums1[j]| + |nums2[i] - nums2[j]| is the smallest amongst all possible indices pairs where i < j.
// Return the beautiful pair.
// In the case that there are multiple beautiful pairs, return the lexicographically smallest pair.
// Note that
// |x| denotes the absolute value of x.
// A pair of indices (i1, j1) is lexicographically smaller than (i2, j2) if i1 < i2 or i1 == i2 and j1 < j2.
// Example 1:
// Input: nums1 = [1,2,3,2,4], nums2 = [2,3,1,2,3]
// Output: [0,3]
// Explanation: Consider index 0 and index 3. The value of |nums1[i]-nums1[j]| + |nums2[i]-nums2[j]| is 1, which is the smallest value we can achieve.
// Example 2:
// Input: nums1 = [1,2,4,3,2,5], nums2 = [1,4,2,3,5,1]
// Output: [1,4]
// Explanation: Consider index 1 and index 4. The value of |nums1[i]-nums1[j]| + |nums2[i]-nums2[j]| is 1, which is the smallest value we can achieve.
// Constraints:
// 2 <= nums1.length, nums2.length <= 10^5
// nums1.length == nums2.length
// 0 <= nums1i <= nums1.length
// 0 <= nums2i <= nums2.length
import "fmt"
import "sort"
func beautifulPair(nums1 []int, nums2 []int) []int {
n := len(nums1)
pl := map[[2]int][]int{}
for i := 0; i < n; i++ {
k := [2]int{nums1[i], nums2[i]}
pl[k] = append(pl[k], i)
}
points := [][3]int{}
for i := 0; i < n; i++ {
k := [2]int{nums2[i], nums1[i]}
if len(pl[k]) > 1 { return []int{pl[k][0], pl[k][1]} }
points = append(points, [3]int{nums1[i], nums2[i], i})
}
sort.Slice(points, func(i, j int) bool {
return points[i][0] < points[j][0]
})
min := func (x, y int) int { if x < y { return x; }; return y; }
max := func (x, y int) int { if x > y { return x; }; return y; }
abs := func(x int) int { if x < 0 { return -x; }; return x; }
distance := func (x1, y1, x2, y2 int) int { return abs(x1-x2) + abs(y1-y2) }
var dfs func(l, r int) [3]int
dfs = func(l, r int) [3]int {
if l >= r { return [3]int{ 1 << 31, -1, -1 } }
m := (l + r) >> 1
x := points[m][0]
t1, t2 := dfs(l, m), dfs(m + 1, r)
if t1[0] > t2[0] || (t1[0] == t2[0] && (t1[1] > t2[1] || (t1[1] == t2[1] && t1[2] > t2[2]))) {
t1 = t2
}
t := [][3]int{}
for i := l; i <= r; i++ {
if abs(points[i][0] - x) <= t1[0] {
t = append(t, points[i])
}
}
sort.Slice(t, func(i, j int) bool {
return t[i][1] < t[j][1]
})
for i := 0; i < len(t); i++ {
for j := i + 1; j < len(t); j++ {
if t[j][1]-t[i][1] > t1[0] { break }
pi, pj := min(t[i][2], t[j][2]), max(t[i][2], t[j][2])
d := distance(t[i][0], t[i][1], t[j][0], t[j][1])
if d < t1[0] || (d == t1[0] && (pi < t1[1] || (pi == t1[1] && pj < t1[2]))) {
t1 = [3]int{d, pi, pj}
}
}
}
return t1
}
res := dfs(0, n-1)
return []int{ res[1], res[2] }
}
func main() {
// Example 1:
// Input: nums1 = [1,2,3,2,4], nums2 = [2,3,1,2,3]
// Output: [0,3]
// Explanation: Consider index 0 and index 3. The value of |nums1[i]-nums1[j]| + |nums2[i]-nums2[j]| is 1, which is the smallest value we can achieve.
fmt.Println(beautifulPair([]int{1,2,3,2,4}, []int{2,3,1,2,3})) // [0,3]
// Example 2:
// Input: nums1 = [1,2,4,3,2,5], nums2 = [1,4,2,3,5,1]
// Output: [1,4]
// Explanation: Consider index 1 and index 4. The value of |nums1[i]-nums1[j]| + |nums2[i]-nums2[j]| is 1, which is the smallest value we can achieve.
fmt.Println(beautifulPair([]int{1,2,4,3,2,5}, []int{1,4,2,3,5,1})) // [1,4]
}