作者: 负雪明烛
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. 蛋白组DIA分析:Spectronaut软件使用指南

    官方文档: https://biognosys.com/media.ashx/spectronautmanual.pdf 0. 准备 Spectronaut软件是蛋白组DIA分析最常用的谱图解析软件之 ...

  2. Excel-vlookup内部能不能用函数?(即内部嵌套函数)

    11.vlookup(查找值,目标区域,列序号,FALSE0/TRUE1)内部能不能用函数?(即内部嵌套函数) 总结:只能说有,但不是所有,目前还没有找到规律(唯一的规律是内嵌函数结果值得是符合vlo ...

  3. Nginx编译参数详细介绍

    /configure --help --help 显示本提示信息 --prefix=PATH 设定安装目录 --sbin-path=PATH 设定程序文件目录 --conf-path=PATH 设定配 ...

  4. UE4之Slate:App默认窗口的创建流程

    UE4版本:4.24.3源码编译 Windows10 + VS2019开发环境 在先前分享的基础上,现在来梳理下App启动时默认窗口的创建流程,以及相关的类.对象之间的抽象层级: 纯C++工程配置 S ...

  5. 出现NoClassDefFoundError,始终无法引入jar的解决

    在拉取代码后,项目的部分版本与本地存在的不一定一致,所以IDEA会自动下载并引入,但是在启动时可能存在java.lang.NoClassDefFoundError这个报错 比如引入marshallin ...

  6. 在Kubernetes上安装MySQL-PXC集群

    官方部署文档地址:https://www.percona.com/doc/kubernetes-operator-for-pxc/kubernetes.html 一.部署方式 示例在k8s集群(至少3 ...

  7. 大数据学习day38----数据仓库01-----区域字典的生成

    更多内容见文档 1. 区域字典的生成 mysql中有如下表格数据 现要将这类数据转换成(GEOHASH码, 省,市,区)如下所示 (1)第一步:在mysql中使用sql语句对表格数据进行整理(此处使用 ...

  8. Swift Storyboard找不到类文件

    Swift语言引入了Module概念,在通过关键字@objc(类名)做转换的时候,由于Storyboard没有及时更新Module属性,会导致如下两种类型错误: 1 用@objc(类名)标记的Swif ...

  9. Android 小知识

    1.判断sd卡是否存在 boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environmen ...

  10. 【Linux】【Services】【VersionControl】git-daemon, httpd, mysql搭建带认证的gitserver

    1. 简介: 比较低端的gitserver,使用centos自带的git-daemon搭建gitserver,使用httpd做上传和下载,利用mod_auth_mysql做认证 2. 环境 # Apa ...