https://leetcode.com/problems/erect-the-fence/
你有n棵树的座标,请你用栅栏把树全部围起来,因为栅栏太贵了所以请围出最短的距离
这题简单来说就是凸包(convex hull)的问题
这个解法是Jarvis' March又叫做礼物包装演算法(Gift Wrapping Algorithm),概念是先找出最外侧的任意点,之後用外积找出其他也是最外侧的点,总之先上程序码吧!
class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
if len(trees) <= 3:
return trees
start = self.find_start_pos(trees)
hull = []
p = start
q = 0
while True:
q = (p + 1) % len(trees)
for i in range(len(trees)):
if self.orientation(trees[p], trees[i], trees[q]) < 0:
# i在逆时钟方向,可以当作最外圈的候选
q = i
for i in range(len(trees)):
if i != p and i != q and self.orientation(trees[p], trees[i], trees[q]) == 0:
# 同一条线上
x = trees[p][0] <= trees[i][0] and trees[i][0] <= trees[q][0] \
or trees[p][0] >= trees[i][0] and trees[i][0] >= trees[q][0]
y = trees[p][1] <= trees[i][1] and trees[i][1] <= trees[q][1] \
or trees[p][1] >= trees[i][1] and trees[i][1] >= trees[q][1]
if x and y:
hull.append(trees[i]);
hull.append(trees[q])
p = q
if p == start:
break
return hull
def find_start_pos(self, trees):
# 找起始的点
start = 0
for i, tree in enumerate(trees):
if math.sqrt(tree[0]**2 + tree[1]**2) < math.sqrt(trees[start][0]**2 + trees[start][1]**2):
start = i
return start
def orientation(self, p, q, r):
#外积
return (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1])
这个方法的时间复查度是 O(mn),m代表最外圈有几个点,如果最外圈点很多的话就会超过系统限制的时间
今天的题目对我来说很难,之前没有接触过凸包的问题,所以学到新东西了
解法1的时间会超过系统要求的限制,所以要用其他的演算法
但是,我懒了
过几天後再补上OK的解法吧!
<<: Day3 理解 golang slice 用法及原理 III
参赛契机 之前参加资策会,结训时都会做个专题啦,但因为我自己对我们组的专题挺不满意,而且对於深度学习...
当获得一个新会员,会希望使用者能够了解如何使用系统,会提供使用者一份指引如何使用,同时也希望使用者尽...
今日专案进度 A. 用grid方式将专案重新排版(透过tailwind css) B. 使用vue-...
你的公司,订好制度了吗? ......还没的话,不要看这篇。 <<< 回头分隔线:...
在学习Encryption 跟Decryption前~ ASCII电脑编码系统是必须要知道的。 AS...