Approach
Breadth-first search
For Maximize the Distance Between Points on a Square, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 71 lines of Python from the credited upstream file 3464.py.
- The implementation visibly relies on sequence storage, work queue.
- No explicit loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1from dataclasses import dataclass2 3 4@dataclass(frozen=True)5class Sequence:6 startX: int7 startY: int8 endX: int9 endY: int10 length: int11 12 def __iter__(self):13 yield self.startX14 yield self.startY15 yield self.endX16 yield self.endY17 yield self.length18 19 20class Solution:21 def maxDistance(self, side: int, points: list[list[int]], k: int) -> int:22 ordered = self._getOrderedPoints(side, points)23 24 def isValidDistance(m: int) -> bool:25 """26 Returns True if we can select `k` points such that the minimum Manhattan27 distance between any two consecutive chosen points is at least `m`.28 """29 dq = collections.deque([Sequence(*ordered[0], *ordered[0], 1)])30 maxLength = 131 32 for i in range(1, len(ordered)):33 x, y = ordered[i]34 startX, startY = ordered[i]35 length = 136 while dq and abs(x - dq[0].endX) + abs(y - dq[0].endY) >= m:37 if (abs(x - dq[0].startX) + abs(y - dq[0].startY) >= m38 and dq[0].length + 1 >= length):39 startX = dq[0].startX40 startY = dq[0].startY41 length = dq[0].length + 142 maxLength = max(maxLength, length)43 dq.popleft()44 dq.append(Sequence(startX, startY, x, y, length))45 46 return maxLength >= k47 48 l = 049 r = side50 51 while l < r:52 m = (l + r + 1) 253 if isValidDistance(m):54 l = m55 else:56 r = m - 157 58 return l59 60 def _getOrderedPoints(self, side: int, points: list[list[int]]) -> list[list[int]]:61 """62 Returns the ordered points on the perimeter of a square of side length63 `side`, starting from left, top, right, and bottom boundaries.64 """65 left = sorted([(x, y) for x, y in points if x == 0 and y > 0])66 top = sorted([(x, y) for x, y in points if x > 0 and y == side])67 right = sorted([(x, y) for x, y in points if x == side and y < side],68 reverse=True)69 bottom = sorted([(x, y) for x, y in points if y == 0], reverse=True)70 return left + top + right + bottom71