leetcode 850. Rectangle Area II
给定一些矩形2 求覆盖面积 矩形不超过200个
1#
算法1 朴素思想 虽然朴素但是代码却有意思
利用容斥原理
复杂度高达 N*2^N
class Solution:
def intersect(rec1,rec2):
return [max(rec1[1],rec2[1]),
max(rec1[2],rec2[2]),
min(rec1[3],rec2[3]),
min(rec1[4],rec2[4]
]
#这里是两个矩形的两点式求 矩形的相交子矩形 *非常值得思考
def area(rec):
dx=max(0,rec[2]-rec[0])
dy=max(0,rec[3]-rec[1])
return dx*dy
ans=0
for size in range(1,len(rectangles)+1):
for group in itertools.combinations(rectangles,size):
ans = ans +(-1)** (size+1) * area(reduce(intersect,group))
return ans%mod
2#
点位压缩,压缩后进行暴力循环
同时压缩x和y
最后返回
class Solution(object):
def rectangleArea(self, rectangles):
N = len(rectangles)
Xvals, Yvals = set(), set()
for x1, y1, x2, y2 in rectangles:
Xvals.add(x1); Xvals.add(x2)
Yvals.add(y1); Yvals.add(y2)
imapx = sorted(Xvals)
imapy = sorted(Yvals)
mapx = {x: i for i, x in enumerate(imapx)}
mapy = {y: i for i, y in enumerate(imapy)}
grid = [[0] * len(imapy) for _ in imapx]
for x1, y1, x2, y2 in rectangles:
for x in xrange(mapx[x1], mapx[x2]):
for y in xrange(mapy[y1], mapy[y2]):
grid[x][y] = 1
ans = 0
for x, row in enumerate(grid):
for y, val in enumerate(row):
if val:
ans += (imapx[x+1] - imapx[x]) * (imapy[y+1] - imapy[y])
return ans % (10**9 + 7)
N^3
3#
算法3 扫描线算法
将每一个矩形看作一个 "事件" 这样的事件
class Solution(object):
def rectangleArea(self, rectangles):
# Populate events
OPEN, CLOSE = 0, 1
events = []
for x1, y1, x2, y2 in rectangles:
events.append((y1, OPEN, x1, x2))
events.append((y2, CLOSE, x1, x2))
events.sort()
def query():
ans = 0
cur = -1
for x1, x2 in active:
cur = max(cur, x1)
ans += max(0, x2 - cur)
cur = max(cur, x2)
return ans
active = []
cur_y = events[0][0]
ans = 0
for y, typ, x1, x2 in events:
# For all vertical ground covered, update answer
ans += query() * (y - cur_y)
# Update active intervals
if typ is OPEN:
active.append((x1, x2))
active.sort()
else:
active.remove((x1, x2))
cur_y = y
return ans % (10**9 + 7)
4#
注意到刚才的3算法中使用了 区间维护的算法 这里使用线段树维护这个区间
使得达到 NlogN
下面是py 实现线段树
class Node:
def __init__(self,start,end):
self.start=start
self.end=end
self.mid=(start+end)//2
self.active_count=0
self.totle =0
self._left=None
self._right=None
@property
def right(self):
self._right= self._right or Node(self.mid,self.end)
return self._right
@property
def left(self):
self._left=self._left or Node(self.start,self.mid)
return self._left
#更新 i j 合适的区域 + val
#同时返回 i j 之间的x大小
def update(self,i,j,val):
print(str(i)+" "+str(j))
if(i>=j):
return 0
if(i==self.start and j==self.end):
self.active_count = self.active_count + val
else:
self.left .update( i, min( self.mid ,j ) , val )
self.right.update( max(self.mid,i) ,j, val )
#当前区域有 至少一个覆盖
if(self.active_count>0):
self.totle= X[self.end]-X[self.start]
else:
self.totle= self.left.totle + self.right.totle
return self.totle
class Solution:
def rectangleArea(self, rectangles):
"""
:type rectangles: List[List[int]]
:rtype: int
"""
ACTIVE = 1
DEACTIVE = -1
global X
X=set()
events=[]
for rect in rectangles:
X.add(rect[0])
X.add(rect[2])
events.append([rect[1],rect[0],rect[2],ACTIVE])
events.append([rect[3],rect[0],rect[2],DEACTIVE])
X=sorted(X)
events=sorted(events)
pos2idx={ x:i for i,x in enumerate(X) }
sum_area=0
y_cur=0
y_cur_next=0
x_cur=0
SegNode = Node(0,len(pos2idx))
print(pos2idx)
for event in events:
y_cur_next=event[0]
sum_area=sum_area+(y_cur_next-y_cur)*x_cur
x_cur=SegNode.update(pos2idx[event[1]],pos2idx[event[2]],event[3])
print(event)
print(x_cur)
y_cur=y_cur_next
return sum_area%(1000000000 + 7)
注意这里的 下标实际意义不是 容器 而是 标志
所以 会有
start mid
mid end
这样的划分方法 应该注意
另外利用python 的 property 很方便的写出了懒申请策略
(python 做点集压缩真的方便
付:
leetcode 56. Merge Intervals On 求overlap
leetcode 850. Rectangle Area II的更多相关文章
- [LeetCode] 850. Rectangle Area II 矩形面积之二
We are given a list of (axis-aligned) rectangles. Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...
- [LeetCode] 223. Rectangle Area 矩形面积
Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...
- leetcode之Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- [Swift]LeetCode850. 矩形面积 II | Rectangle Area II
We are given a list of (axis-aligned) rectangles. Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...
- Java for LeetCode 223 Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- (easy)LeetCode 223.Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- leetcode:Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- Java [Leetcode 223]Rectangle Area
题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...
- LeetCode(41)-Rectangle Area
题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...
随机推荐
- “fixed+relative≈≈absolute”——对BFC的再次思考
好久没写博客了,刚好今天跨年夜没约到什么妹子,在家宅着不如写点东西好了. 需求 昨天晚上,给公司年会做一个移动端的投票页面,遇到一个UI优化的问题: · 正文内容少于一屏时,投票提交按钮固定显示在页面 ...
- <每日一题>算法题:集合求并集并排序
题目描述 给你两个集合,要求{A} + {B}. 注:同一个集合中不会有两个相同的元素. 输入描述: 每组输入数据分为三行,第一行有两个数字n,m(0 ≤ n,m ≤ 10000),分别表示集合A和集 ...
- 侧滑关闭Activity的解决方案——SwipeBackLayout
项目地址:ikew0ng/SwipeBackLayout: An Android library that help you to build app with swipe back gesture. ...
- UMP系统架构 Zookeeper
- kali linux 入门(1) 基于win10和docker的环境搭建
1. 前言 渗透测试并没有一个标准的定义.国外一些安全组织达成共识的通用说法是,渗透测试是通过模拟恶意黑客的攻击方法,来评估计算机网络系统安全的一种评估方法,这个过程包括对系统的任何弱点.技术缺陷或漏 ...
- Leetcode93. Restore IP Addresses复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...
- redis笔记_源码_内存分配
文件:zmoalloc.h zmoalloc.c 1.求两个整数的余数 eg: 求_n对sizeof(long)的余数(_n&(sizeof(long)-1)), 性能提升为50%-100% ...
- Apache服务器中运行CGI程序的方法,文中以Perl脚本作为示例
关于apache与CGI在这里就不解释了. 1.apache下面以2.0.63为例介绍运行CGI程序的配置.(http://www.nklsyy.com) 2.下载Windows下的Perl解释器Ac ...
- 廖雪峰Java12maven基础-2maven进阶-2模块管理
1. 把大项目拆分为模块是降低软件复杂度的有效方法 在Java项目中,我们通常会会把一个项目分拆为模块,这是为了降低软件复杂度. 例如:我们可以把一个大的项目氛围module-a, module-b, ...
- 数据结构学习笔记_树(二叉搜索树,B-树,B+树,B*树)
一.查找二叉树(二叉搜索树BST) 1.查找二叉树的性质 1).所有非叶子结点至多拥有两个儿子(Left和Right): 2).所有结点存储一个关键字: 3).非叶子结点的左指针指向小于其关键字的子树 ...