-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3464-MaximizeTheDistanceBetweenPointsOnASquare.go
More file actions
107 lines (95 loc) · 3.8 KB
/
3464-MaximizeTheDistanceBetweenPointsOnASquare.go
File metadata and controls
107 lines (95 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
101
102
103
104
105
106
107
package main
// 3464. Maximize the Distance Between Points on a Square
// You are given an integer side, representing the edge length of a square with corners at (0, 0), (0, side), (side, 0), and (side, side) on a Cartesian plane.
// You are also given a positive integer k and a 2D integer array points, where points[i] = [xi, yi] represents the coordinate of a point lying on the boundary of the square.
// You need to select k elements among points such that the minimum Manhattan distance between any two points is maximized.
// Return the maximum possible minimum Manhattan distance between the selected k points.
// The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.
// Example 1:
// Input: side = 2, points = [[0,2],[2,0],[2,2],[0,0]], k = 4
// Output: 2
// Explanation:
// <img src="https://assets.leetcode.com/uploads/2025/01/28/4080_example0_revised.png" />
// Select all four points.
// Example 2:
// Input: side = 2, points = [[0,0],[1,2],[2,0],[2,2],[2,1]], k = 4
// Output: 1
// Explanation:
// <img src="https://assets.leetcode.com/uploads/2025/01/28/4080_example1_revised.png" />
// Select the points (0, 0), (2, 0), (2, 2), and (2, 1).
// Example 3:
// Input: side = 2, points = [[0,0],[0,1],[0,2],[1,2],[2,0],[2,2],[2,1]], k = 5
// Output: 1
// Explanation:
// <img src="https://assets.leetcode.com/uploads/2025/01/28/4080_example2_revised.png" />
// Select the points (0, 0), (0, 1), (0, 2), (1, 2), and (2, 2).
// Constraints:
// 1 <= side <= 10^9
// 4 <= points.length <= min(4 * side, 15 * 10^3)
// points[i] == [xi, yi]
// The input is generated such that:
// points[i] lies on the boundary of the square.
// All points[i] are unique.
// 4 <= k <= min(25, points.length)
import "fmt"
import "sort"
func maxDistance(side int, points [][]int, k int) int {
n := len(points)
arr := make([]int, n)
for i, p := range points {
x, y := p[0], p[1]
if x == 0 {
arr[i] = y
} else if y == side {
arr[i] = side + x
} else if x == side {
arr[i] = side * 3 - y
} else {
arr[i] = side * 4 - x
}
}
sort.Ints(arr)
f, end := make([]int, n + 1), make([]int, n)
return sort.Search(side, func(low int) bool {
low++
j := n
for i := n - 1; i >= 0; i-- {
for arr[j - 1] >= arr[i] + low {
j--
}
f[i] = f[j] + 1
if f[i] == 1 {
end[i] = i // i 自己就是最后一个点
} else {
end[i] = end[j]
}
if f[i] > k || f[i] == k && arr[end[i]] - arr[i] <= side * 4-low {
return false
}
}
return true
})
}
func main() {
// Example 1:
// Input: side = 2, points = [[0,2],[2,0],[2,2],[0,0]], k = 4
// Output: 2
// Explanation:
// <img src="https://assets.leetcode.com/uploads/2025/01/28/4080_example0_revised.png" />
// Select all four points.
fmt.Println(maxDistance(2, [][]int{{0,2},{2,0},{2,2},{0,0}}, 4)) // 4
// Example 2:
// Input: side = 2, points = [[0,0],[1,2],[2,0],[2,2],[2,1]], k = 4
// Output: 1
// Explanation:
// <img src="https://assets.leetcode.com/uploads/2025/01/28/4080_example1_revised.png" />
// Select the points (0, 0), (2, 0), (2, 2), and (2, 1).
fmt.Println(maxDistance(2, [][]int{{0,0},{1,2},{2,0},{2,2},{2,1}}, 4)) // 1
// Example 3:
// Input: side = 2, points = [[0,0],[0,1],[0,2],[1,2],[2,0],[2,2],[2,1]], k = 5
// Output: 1
// Explanation:
// <img src="https://assets.leetcode.com/uploads/2025/01/28/4080_example2_revised.png" />
// Select the points (0, 0), (0, 1), (0, 2), (1, 2), and (2, 2).
fmt.Println(maxDistance(2, [][]int{{0,0},{0,1},{0,2},{1,2},{2,0},{2,2},{2,1}}, 5)) // 1
}