- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 46 lines of Python from the credited upstream file 3102.py.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution:2 def minimumDistance(self, points: list[list[int]]) -> int:3 i, j = self._maxManhattanDistance(points, -1)4 xi, yi = self._maxManhattanDistance(points, i)5 xj, yj = self._maxManhattanDistance(points, j)6 return min(self._manhattan(points, xi, yi),7 self._manhattan(points, xj, yj))8 9 def _maxManhattanDistance(10 self,11 points: list[list[int]],12 excludedIndex: int,13 ) -> int:14 minSum = math.inf15 maxSum = -math.inf16 minDiff = math.inf17 maxDiff = -math.inf18 minSumIndex = -119 maxSumIndex = -120 minDiffIndex = -121 maxDiffIndex = -122 23 for i, (x, y) in enumerate(points):24 if i == excludedIndex:25 continue26 summ = x + y27 diff = x - y28 if summ < minSum:29 minSum = summ30 minSumIndex = i31 if summ > maxSum:32 maxSum = summ33 maxSumIndex = i34 if diff < minDiff:35 minDiff = diff36 minDiffIndex = i37 if diff > maxDiff:38 maxDiff = diff39 maxDiffIndex = i40 41 return ([minSumIndex, maxSumIndex] if maxSum - minSum >= maxDiff - minDiff42 else [minDiffIndex, maxDiffIndex])43 44 def _manhattan(self, points: list[list[int]], i: int, j: int) -> int:45 return abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1])46