|
| 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