Exclude each point
We can find the max distance between pairs of points excluding each point and take the minimum.
Time: , space:
.
class Solution:
def max_distance_without(self, excluded, points):
def manhattan_distance(a, b):
return abs(a[0]-b[0]) + abs(a[1]-b[1])
max_distance = 0
for i in range(len(points)):
if i == excluded:
continue
for j in range(i+1, len(points)):
if j == excluded:
continue
max_distance = max( max_distance, manhattan_distance( points[i], points[j] ) )
return max_distance
def minimumDistance(self, points: List[List[int]]) -> int:
min_distance = float("inf")
for excluded in range(len(points)):
min_distance = min( min_distance, self.max_distance_without(excluded, points) )
return min_distance
Extreme points
We want to remove one of the “extreme points” because extreme points are the furthest apart from each other.
Say we have a pair of points: . The Manhattan distance between the pair of points:
. To find the pair of points that are furthest apart from each other, we cannot just look at the distance in one of the coordinates. Because, the pair of points furthest apart in x-coordinate might be very close in y-coordinate. So, we reformulate the Manhattan distance using an alternative definition of absolute value. The reformulation casts the points in a different coordinate system where, we can find the the pair of points furthest apart from each other by looking at one coordinate at a time.
The usual definition of the absolute value function is:
Alternatively, we can also write:
.
With the alternative definition of absolute value function, the Manhattan distance becomes:
We have two choices for the first and two choices for the second
. So, the Manhattan distance is the maximum among four possible values:
We can rearrange the terms to get:
All four terms now involve sum or difference between a point’s coordinates. Say and
. So,
and
are the two coordinates of the point
in the new coodinate system.
Finally, .
The extreme points for the Manhattan distance are the points having . We should remove one of these four points. To find the distance after removing one of these points, we also keep track of
.
Time: , space:
.
class Solution:
def minimumDistance(self, points: List[List[int]]) -> int:
sum_max = sum_max2 = diff_max = diff_max2 = float("-inf")
sum_max_i = diff_max_i = None
sum_min = sum_min2 = diff_min = diff_min2 = float("inf")
sum_min_i = diff_min_i = None
for i, p in enumerate(points):
x, y = p
sum = x + y
if sum > sum_max:
sum_max, sum_max2 = sum, sum_max
sum_max_i = i
elif sum > sum_max2:
sum_max2 = sum
if sum < sum_min:
sum_min, sum_min2 = sum, sum_min
sum_min_i = i
elif sum < sum_min2:
sum_min2 = sum
diff = x - y
if diff > diff_max:
diff_max, diff_max2 = diff, diff_max
diff_max_i = i
elif diff > diff_max2:
diff_max2 = diff
if diff < diff_min:
diff_min, diff_min2 = diff, diff_min
diff_min_i = i
elif diff < diff_min2:
diff_min2 = diff
min_dist_removed = float("inf")
for removed_i in (sum_max_i, sum_min_i, diff_max_i, diff_min_i):
max_sum_removed = sum_max2 if removed_i == sum_max_i else sum_max
min_sum_removed = sum_min2 if removed_i == sum_min_i else sum_min
max_diff_removed = diff_max2 if removed_i == diff_max_i else diff_max
min_diff_removed = diff_min2 if removed_i == diff_min_i else diff_min
min_dist_removed = min(
min_dist_removed,
max(
max_sum_removed - min_sum_removed,
max_diff_removed - min_diff_removed,
),
)
return min_dist_removed
Leave a comment