作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/problems/rectangle-area/description/

题目描述:

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Example:

Input: A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2
Output: 45

Note:

Assume that the total area is never beyond the maximum possible value of int.

题目大意

求两个矩形覆盖的总面积。

解题方法

很好理解,总面积等于两个矩形的面积和 - 公共面积。实际上就是让我们求公共面积。

拿到两个矩形的问题,我一般都会先对坐标进行排序,这样的好处是可以使两者的位置相对固定,按照相对顺序的模式求解。排序的方式就是按照四个坐标对应关系去排。因为总的只有两个矩形,排序这步速度很快。

但是这个题并不需要排序也可以,因为求公共面积使用了最小最大值关系,所以没必要排序。事实上,排不排序这一步对时间没有影响,都是56ms,因此我建议还是排序。

公共面积等于(min(C, G) - max(A, E)) × (min(D, H) - max(B, F)),从图中很容易看出来。就不讲了。需要注意的是不能把两个直接相乘,因为当两个矩形不想交的时候,这两个部分可能都是负值,相乘得到了正值,误以为相交了,其实没有。所以需要一个判断。

时间复杂度是O(1),空间复杂度是O(1)。

class Solution(object):
def computeArea(self, A, B, C, D, E, F, G, H):
"""
:type A: int
:type B: int
:type C: int
:type D: int
:type E: int
:type F: int
:type G: int
:type H: int
:rtype: int
"""
points = [((A, B), (C, D)), ((E, F), (G, H))]
points.sort()
((A, B), (C, D)), ((E, F), (G, H)) = points
area1 = (D - B) * (C - A)
area2 = (H - F) * (G - E)
x, y = (min(C, G) - max(A, E)), (min(D, H) - max(B, F))
area = 0
if x > 0 and y > 0:
area = x * y
return area1 + area2 - area

参考资料:

日期

2018 年 10 月 8 日 —— 终于开学了。

【LeetCode】223. Rectangle Area 解题报告(Python)的更多相关文章

  1. [LeetCode] 223. Rectangle Area 矩形面积

    Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...

  2. Java for LeetCode 223 Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  3. Java [Leetcode 223]Rectangle Area

    题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...

  4. (easy)LeetCode 223.Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  5. LeetCode : 223. Rectangle Area

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw

  6. LeetCode 223 Rectangle Area(矩形面积)

    翻译 找到在二维平面中两个相交矩形的总面积. 每一个矩形都定义了其左下角和右上角的坐标. (矩形例如以下图) 如果,总占地面积永远不会超过int的最大值. 原文 分析 这题前天试过,写了一堆推断.终究 ...

  7. [LeetCode]223. Rectangle Area矩形面积

    /* 像是一道数据分析题 思路就是两个矩形面积之和减去叠加面积之和 */ public int computeArea(int A, int B, int C, int D, int E, int F ...

  8. 【LeetCode】836. Rectangle Overlap 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rectangle ...

  9. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

随机推荐

  1. linux 两服务器之间的文件传输scp

    Linux scp 命令用于 Linux 之间复制文件和目录. scp 是 secure copy 的缩写, scp 是 linux 系统下基于 ssh 登陆进行安全的远程文件拷贝命令. scp 是加 ...

  2. 利用plink软件基于LD信息过滤SNP

    最近有需求,对WGS测序获得SNP信息进行筛减,可问题是测序个体少,call rate,maf,hwe,等条件过滤后,snp数量还是千万级别,所以后面利用plink工具根据LD信息来滤除大量SNP标记 ...

  3. D3学习-加载本地数据

    在加载本地数据时,弄了很久都无法显示出来,后来才知道是要把数据文件和html文件都加载到服务器上面 这样就可以显示出来了,

  4. linux下怎么查看某个命令属于哪个包

    # yum whatprovides */ip  或者 # yum provides */ip 即可

  5. Android 基础UI组件(二)

    1.Spinner 提供一个快速的方法来从一组值中选择一个值.在默认状态Spinner显示当前选择的值.触摸Spinner与所有其他可用值显示一个下拉菜单,可以选择一个新的值. /** * 写死内容: ...

  6. 【Go】【Basic】MacOS上搭建GO开发环境

    1. GO下载 1.1. 下载地址:https://www.golangtc.com/download (需要科学上网) 1.1.1. PKG安装: 下载这个包:go1.9.2.darwin-amd6 ...

  7. Docker 生产环境之配置容器 - 自动启动容器

    原文地址 Docker 提供了重启策略,以控制容器在退出时是否自动启动,或在 Docker 重新启动时自动启动.重启策略可确保链接的容器以正确的顺序启动.Docker 建议使用重启策略,并避免使用流程 ...

  8. String.split()与StringUtils.split()的区别

    import com.sun.deploy.util.StringUtils; String s =",1,,2,3,4,,"; String[] split1 = s.split ...

  9. Dubbo使用Zookeeper注册中心

    在生产环境下使用最多的注册中心为Zookeeper,当然,Redis也可以做注册中心 一.创建提供者02-provider-zk (1) 导入依赖 https://blog.csdn.net/u012 ...

  10. 【Linux】【Basis】用户、组和权限管理

    1. 概念: 1.1. 每个使用者都有,用户标识UID和密码:并且有身份(Authentication),授权(Authorization),审计(Audition):组(用户组,用户容器) 1.2. ...