Open rbee3u opened 6 years ago
edge_set
其实是没有必要的,每次添加前从当前集合中遍历就行了,加了它反而显得复杂了些。
强迫症再来提交一发简洁一些的答案
# coding: utf-8
class Solution(object):
def __init__(self):
pass
def maximumRectangles(self, rects):
def do_covering(x, y):
(xu, xd, xl, xr), (yu, yd, yl, yr) = x, y
if xu<yd or yu<xd or xr<yl or yr<xl:
return None
return ( max(xu, yu), min(xd, yd) \
, min(xl, yl), max(xr, yr) )
result, length = [x for x in rects], len(rects)
i = 0
while i < length:
for j in range(i):
temp = do_covering(result[i], result[j])
if temp:
result[i] = temp
del result[j]
i = i - 2
length = length - 1
break
i = i + 1
return result
if __name__ == '__main__':
rect_list = [
(1, 0, 0, 1),
(1, 0, 4, 5),
(3, 2, 2, 5),
(2, 1, 5, 6)
]
sol = Solution()
ret = sol.maximumRectangles(rect_list)
print(ret)
前面的没太看懂容我回去瞅瞅, 简洁版简单易懂赞赞赞 唯一的建议就是别把多行逻辑写一行, 虽然这样好看一些
@isaacpei 好好好 = =,,,强迫惯了,多行写一行确实坑,我改一下
先赞后看
Question_ID: Q003
Language: Python3
Time Cost: 卡在去找低于平方的算法,远远超过40分钟了
Time Complexity: O(n^2)
Solution
rect_dict
和一个存储相交关系的集合edge_set
;O(n)
的,整个合并的过程是O(n^2)
。剩下一个问题就是从相交集合中取出矩形对的次数,可以考虑取出次数等于添加进去的次数,而每次合并添加的次数也是O(n)
的,因此取出矩形对的次数也是O(n^2)
的;O(n^2)
的(这里把字典与集合的增删查改视为O(1)
)。My Code
Other