【LeetCode】223. Rectangle Area 解题报告(Python)
作者: 负雪明烛
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)的更多相关文章
- [LeetCode] 223. Rectangle Area 矩形面积
Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...
- Java for LeetCode 223 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 ...
- (easy)LeetCode 223.Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- LeetCode : 223. Rectangle Area
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw
- LeetCode 223 Rectangle Area(矩形面积)
翻译 找到在二维平面中两个相交矩形的总面积. 每一个矩形都定义了其左下角和右上角的坐标. (矩形例如以下图) 如果,总占地面积永远不会超过int的最大值. 原文 分析 这题前天试过,写了一堆推断.终究 ...
- [LeetCode]223. Rectangle Area矩形面积
/* 像是一道数据分析题 思路就是两个矩形面积之和减去叠加面积之和 */ public int computeArea(int A, int B, int C, int D, int E, int F ...
- 【LeetCode】836. Rectangle Overlap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rectangle ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
随机推荐
- R连接mysql数据库方法详解
Warning messages: 1: In odbcDriverConnect("DSN=Rdata;UID=root") : [RODBC] ERROR: state IM0 ...
- linux系统中tomcat的安装及使用
linux系统中tomcat的安装及使用 linux系统中安装tomcat tar.gz/tar文件格式安装 先下载好该文件,将文件放置在校安装的目录下, 如果是tar.gz后缀使用 tar -zxv ...
- 试了下GoAsm
在VC里我们: #include <windows.h> DWORD dwNumberOfBytesWritten; int main() { HANDLE hStdOut = GetSt ...
- k8s使用ceph的rbd作后端存储
k8s使用rbd作后端存储 k8s里的存储方式主要有三种.分别是volume.persistent volumes和dynamic volume provisioning. volume: 就是直接挂 ...
- IDEA中对代码进行测试
一. 建立对应得目录 二.导入junit依赖 <dependency> <groupId>junit</groupId> <artifactId>jun ...
- 内存管理——placement new
C++给我们三个申请内存的方式,new(new operator),array new 和placement new. placement new意思是 让对象构建在已经分配好的内存上. (这里我再把 ...
- Shell学习(七)——sort、uniq、cut、wc命令详解
Shell学习(七)--sort.uniq.cut.wc命令详解 转自:[1]linux sort,uniq,cut,wc命令详解 https://www.cnblogs.com/ggjucheng/ ...
- jenkins之邮箱设置
- Static data members in C++
Predict the output of following C++ program: 1 #include <iostream> 2 using namespace std; 3 4 ...
- Spring(1):Spring介绍
一,Spring简介: Spring是一个开源框架,它由Rod Johnson创建:它是为了解决企业应用开发的复杂性而创建的 Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架 ...