Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 352d858

Browse files
committedMar 29, 2025
Add solution #3464
1 parent 9e1ee64 commit 352d858

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,060 LeetCode solutions in JavaScript
1+
# 1,061 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -1066,6 +1066,7 @@
10661066
3461|[Check If Digits Are Equal in String After Operations I](./solutions/3461-check-if-digits-are-equal-in-string-after-operations-i.js)|Easy|
10671067
3462|[Maximum Sum With at Most K Elements](./solutions/3462-maximum-sum-with-at-most-k-elements.js)|Medium|
10681068
3463|[Check If Digits Are Equal in String After Operations II](./solutions/3463-check-if-digits-are-equal-in-string-after-operations-ii.js)|Hard|
1069+
3464|[Maximize the Distance Between Points on a Square](./solutions/3464-maximize-the-distance-between-points-on-a-square.js)|Hard|
10691070

10701071
## License
10711072

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* 3464. Maximize the Distance Between Points on a Square
3+
* https://leetcode.com/problems/maximize-the-distance-between-points-on-a-square/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer side, representing the edge length of a square with corners
7+
* at (0, 0), (0, side), (side, 0), and (side, side) on a Cartesian plane.
8+
*
9+
* You are also given a positive integer k and a 2D integer array points, where
10+
* points[i] = [xi, yi] represents the coordinate of a point lying on the boundary of
11+
* the square.
12+
*
13+
* You need to select k elements among points such that the minimum Manhattan distance
14+
* between any two points is maximized.
15+
*
16+
* Return the maximum possible minimum Manhattan distance between the selected k points.
17+
*
18+
* The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.
19+
*/
20+
21+
/**
22+
* @param {number} squareSide
23+
* @param {number[][]} coordinates
24+
* @param {number} pointsToPlace
25+
* @return {number}
26+
*/
27+
var maxDistance = function(squareSide, coordinates, pointsToPlace) {
28+
const pointCount = coordinates.length;
29+
const positions = new Array(pointCount);
30+
31+
for (let i = 0; i < pointCount; i++) {
32+
const [x, y] = coordinates[i];
33+
positions[i] = y === 0 ? x : x === squareSide
34+
? squareSide + y
35+
: y === squareSide
36+
? 2 * squareSide + (squareSide - x)
37+
: 3 * squareSide + (squareSide - y);
38+
}
39+
positions.sort((a, b) => a - b);
40+
41+
const perimeter = 4 * squareSide;
42+
const extendedPositions = new Array(pointCount * 2);
43+
for (let i = 0; i < pointCount; i++) {
44+
extendedPositions[i] = positions[i];
45+
extendedPositions[i + pointCount] = positions[i] + perimeter;
46+
}
47+
48+
let result = 0;
49+
let maxDistance = 2 * squareSide;
50+
while (result < maxDistance) {
51+
const midDistance = Math.floor((result + maxDistance + 1) / 2);
52+
if (canPlaceAtDistance(midDistance)) result = midDistance;
53+
else maxDistance = midDistance - 1;
54+
}
55+
return result;
56+
57+
function canPlaceAtDistance(distance) {
58+
for (let start = 0; start < pointCount; start++) {
59+
let current = start;
60+
let lastPos = extendedPositions[start];
61+
const limit = start + pointCount;
62+
let valid = true;
63+
64+
for (let placed = 1; placed < pointsToPlace; placed++) {
65+
const nextTarget = lastPos + distance;
66+
let left = current + 1;
67+
let right = limit;
68+
69+
while (left < right) {
70+
const mid = Math.floor((left + right) / 2);
71+
if (extendedPositions[mid] < nextTarget) left = mid + 1;
72+
else right = mid;
73+
}
74+
75+
if (left === limit) {
76+
valid = false;
77+
break;
78+
}
79+
current = left;
80+
lastPos = extendedPositions[current];
81+
}
82+
83+
if (valid && extendedPositions[start] + perimeter - lastPos >= distance) {
84+
return true;
85+
}
86+
}
87+
return false;
88+
}
89+
};

0 commit comments

Comments
 (0)
Please sign in to comment.