1. 解题思路
这一题比较惭愧,没有自己搞定,是看了大佬们的答案之后才搞定的,而且即便目前算是搞定了,其实思路上还是没有完全想明白,就有点坑爹。
这道题的话大佬们的思路其实偏向于脑筋急转弯,我们不用费劲去考虑两两之间点的距离,而是只需要考虑,要删除一个点使得剩余点的最大曼哈顿距离最小,那么对应删掉的这个点一定在最外侧,也就只能在左上、左下、右上、右下四个角上。
因此,我们只需要分别考察删除这四个位置的点之后剩余点之间的最大距离,然后从中选择最小的结果返回即可。
2. 代码实现
给出python代码实现如下:
class Solution:
def minimumDistance(self, points: List[List[int]]) -> int:
def get_max_distance(points):
max_top_right = max(x+y for x, y in points)
min_bottom_left = min(x+y for x, y in points)
max_top_left = max(y-x for x, y in points)
max_bottom_right = min(y-x for x, y in points)
return max(max_top_right-min_bottom_left, max_top_left - max_bottom_right)
possible = []
points = sorted(points, key=lambda x: x[0] + x[1])
possible.append(get_max_distance(points[1:]))
possible.append(get_max_distance(points[:-1]))
points = sorted(points, key=lambda x: x[1] - x[0])
possible.append(get_max_distance(points[1:]))
possible.append(get_max_distance(points[:-1]))
return min(possible)
提交代码评测得到:耗时1862ms,占用内存64.2MB。
但是,虽然这个代码能够通过所有测试样例,我还是对这里的思路多少有点疑惑,一下子表达不好,就暂时不说了,后面再想想吧,总感觉欠缺一个严格的证明。