|
| 1 | +package g3401_3500.s3464_maximize_the_distance_between_points_on_a_square; |
| 2 | + |
| 3 | +// #Hard #2025_02_23_Time_222_ms_(100.00%)_Space_52.33_MB_(100.00%) |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | + |
| 7 | +public class Solution { |
| 8 | + public int maxDistance(int sideLength, int[][] points, int requiredPoints) { |
| 9 | + long perimeter = 4L * sideLength; |
| 10 | + int numPoints = points.length; |
| 11 | + long[] mappedPositions = new long[numPoints]; |
| 12 | + for (int i = 0; i < numPoints; i++) { |
| 13 | + mappedPositions[i] = mapToPerimeter(sideLength, points[i][0], points[i][1]); |
| 14 | + } |
| 15 | + Arrays.sort(mappedPositions); |
| 16 | + long low = 0; |
| 17 | + long high = perimeter; |
| 18 | + while (low < high) { |
| 19 | + long mid = (low + high + 1) / 2; |
| 20 | + if (isValidDistance(mid, requiredPoints, mappedPositions, perimeter)) { |
| 21 | + low = mid; |
| 22 | + } else { |
| 23 | + high = mid - 1; |
| 24 | + } |
| 25 | + } |
| 26 | + return (int) low; |
| 27 | + } |
| 28 | + |
| 29 | + private long mapToPerimeter(int sideLength, int x, int y) { |
| 30 | + if (y == sideLength) { |
| 31 | + return x; |
| 32 | + } |
| 33 | + if (x == sideLength) { |
| 34 | + return sideLength + (sideLength - y); |
| 35 | + } |
| 36 | + if (y == 0) { |
| 37 | + return 2L * sideLength + (sideLength - x); |
| 38 | + } |
| 39 | + return 3L * sideLength + y; |
| 40 | + } |
| 41 | + |
| 42 | + private boolean isValidDistance( |
| 43 | + long minDistance, int requiredPoints, long[] mappedPositions, long perimeter) { |
| 44 | + int numPoints = mappedPositions.length; |
| 45 | + long[] extendedPositions = new long[2 * numPoints]; |
| 46 | + for (int i = 0; i < numPoints; i++) { |
| 47 | + extendedPositions[i] = mappedPositions[i]; |
| 48 | + extendedPositions[i + numPoints] = mappedPositions[i] + perimeter; |
| 49 | + } |
| 50 | + for (int i = 0; i < numPoints; i++) { |
| 51 | + if (canSelectRequiredPoints( |
| 52 | + i, |
| 53 | + minDistance, |
| 54 | + requiredPoints, |
| 55 | + mappedPositions, |
| 56 | + extendedPositions, |
| 57 | + perimeter)) { |
| 58 | + return true; |
| 59 | + } |
| 60 | + } |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + private boolean canSelectRequiredPoints( |
| 65 | + int startIndex, |
| 66 | + long minDistance, |
| 67 | + int requiredPoints, |
| 68 | + long[] mappedPositions, |
| 69 | + long[] extendedPositions, |
| 70 | + long perimeter) { |
| 71 | + int selectedCount = 1; |
| 72 | + long previousPosition = mappedPositions[startIndex]; |
| 73 | + int currentIndex = startIndex; |
| 74 | + for (int i = 1; i < requiredPoints; i++) { |
| 75 | + long targetPosition = previousPosition + minDistance; |
| 76 | + int left = currentIndex + 1; |
| 77 | + int right = startIndex + mappedPositions.length; |
| 78 | + int nextIndex = lowerBound(extendedPositions, left, right, targetPosition); |
| 79 | + if (nextIndex >= right) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + selectedCount++; |
| 83 | + previousPosition = extendedPositions[nextIndex]; |
| 84 | + currentIndex = nextIndex; |
| 85 | + } |
| 86 | + return selectedCount == requiredPoints |
| 87 | + && (previousPosition - mappedPositions[startIndex] <= perimeter - minDistance); |
| 88 | + } |
| 89 | + |
| 90 | + private int lowerBound(long[] arr, int left, int right, long target) { |
| 91 | + while (left < right) { |
| 92 | + int mid = left + (right - left) / 2; |
| 93 | + if (arr[mid] >= target) { |
| 94 | + right = mid; |
| 95 | + } else { |
| 96 | + left = mid + 1; |
| 97 | + } |
| 98 | + } |
| 99 | + return left; |
| 100 | + } |
| 101 | +} |
0 commit comments