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

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

Example:

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

Note:

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

Credits:
Special thanks to @mithmatt for adding this problem, creating the above image and all test cases.

这道题不算一道很难的题,但博主还是花了很久才做出来,刚开始尝试找出所以有重叠的情况,发现有很多种情况,很麻烦。后来换了一种思路,尝试先找出所有的不相交的情况,只有四种,一个矩形在另一个的上下左右四个位置不重叠,这四种情况下返回两个矩形面积之和。其他所有情况下两个矩形是有交集的,这时候只要算出长和宽,即可求出交集区域的大小,然后从两个矩型面积之和中减去交集面积就是最终答案。求交集区域的长和宽也不难,由于交集都是在中间,所以横边的左端点是两个矩形左顶点横坐标的较大值,右端点是两个矩形右顶点的较小值,同理,竖边的下端点是两个矩形下顶点纵坐标的较大值,上端点是两个矩形上顶点纵坐标的较小值。之前是可以直接将 sum1 和 sum2 加起来的,可以后来OJ搞了些恶心的 test case,使得直接把 sum1 和 sum2 相加会溢出,所以只好分开了,若两个矩形有重叠,那么返回的时候也不能让 sum1 和 sum2 直接相加,中间一定要先减去重叠部分才行,代码如下:

解法一:

  1. class Solution {
  2. public:
  3. int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
  4. int sum1 = (C - A) * (D - B), sum2 = (H - F) * (G - E);
  5. if (E >= C || F >= D || B >= H || A >= G) return sum1 + sum2;
  6. return sum1 - ((min(G, C) - max(A, E)) * (min(D, H) - max(B, F))) + sum2;
  7. }
  8. };

原本上面解法的三行还可以丧心病狂地合成一行,但是由于 OJ 使坏,加了些变态的 test case,使得我们还是得拆分开来,先求出重叠区间的四个点 left,bottom,right,top 的值,然后再求出重叠区间的面积,避免溢出,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
  4. int left = max(A,E), right = max(min(C,G), left);
  5. int bottom = max(B,F), top = max(min(D,H), bottom);
  6. return (C - A) * (D - B) - (right - left) * (top - bottom) + (G - E) * (H - F);
  7. }
  8. };

Github 同步地址:

https://github.com/grandyang/leetcode/issues/223

类似题目:

Rectangle Overlap

Rectangle Area II

参考资料:

https://leetcode.com/problems/rectangle-area/

https://leetcode.com/problems/rectangle-area/discuss/62149/Just-another-short-way

https://leetcode.com/problems/rectangle-area/discuss/62302/Clean-C%2B%2B-Solution-with-Detailed-Explanations

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Rectangle Area 矩形面积的更多相关文章

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

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

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

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

  3. 223 Rectangle Area 矩形面积

    在二维平面上计算出两个由直线构成的矩形叠加覆盖后的面积. 假设面积不会超出int的范围. 详见:https://leetcode.com/problems/rectangle-area/descrip ...

  4. [LeetCode] Rectangle Overlap 矩形重叠

    A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...

  5. [leetcode] Rectangle Area

    Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectang ...

  6. LeetCode Rectangle Area (技巧)

    题意: 分别给出两个矩形的左下点的坐标和右上点的坐标,求他们覆盖的矩形面积? 思路: 不需要模拟,直接求重叠的部分的长宽就行了.问题是如果无重叠部分,注意将长/宽给置为0. class Solutio ...

  7. [HDU 4419] Colourful Rectangle (扫描线 矩形面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题 ...

  8. LeetCode——Rectangle Area

    Description:https://leetcode.com/problems/rectangle-area/ public class Solution { public int compute ...

  9. [LeetCode] 850. Rectangle Area II 矩形面积之二

    We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...

随机推荐

  1. SQL Server 统计信息更新时采样百分比对数据预估准确性的影响

    为什么要写统计信息 最近看到园子里有人写统计信息,楼主也来凑热闹. 话说经常做数据库的,尤其是做开发的或者优化的,统计信息造成的性能问题应该说是司空见惯. 当然解决办法也并非一成不变,“一招鲜吃遍天” ...

  2. TeamCity : 自动触发 Build

    创建了 build 的配置以后,您既可以手动点击 "Run" 按钮来触发一次 build 过程,也可以通过 Triggers 配置实现自动触发 build 过程.一个 trigge ...

  3. MAC终端命令行下用sublime、vscode、atom打开文件或目录

    要知道,有时候一些小技巧,能极大的加大我们的工作效率. 在MAC下开发,用的最多的还是终端,我的终端环境是iterm2+ohmyzsh:步入正题前先给大家介绍几个小技巧: 第一个: 打开findle, ...

  4. 微服务(Microservices)—Martin Fowler【翻译】

    本文转载自:http://www.cnblogs.com/liuning8023/p/4493156.html -------------------------------------------- ...

  5. 初识Hadoop

    第一部分:              初识Hadoop 一.             谁说大象不能跳舞 业务数据越来越多,用关系型数据库来存储和处理数据越来越感觉吃力,一个查询或者一个导出,要执行很长 ...

  6. 手把手教从零开始在GitHub上使用Hexo搭建博客教程(三)-使用Travis自动部署Hexo(1)

    前言 前面两篇文章介绍了在github上使用hexo搭建博客的基本环境和hexo相关参数设置等. 基于目前,博客基本上是可以完美运行了. 但是,有一点是不太好,就是源码同步问题,如果在不同的电脑上写文 ...

  7. LINQ to SQL语句(17)之对象加载

    对象加载 延迟加载 在查询某对象时,实际上你只查询该对象.不会同时自动获取这个对象.这就是延迟加载. 例如,您可能需要查看客户数据和订单数据.你最初不一定需要检索与每个客户有关的所有订单数据.其优点是 ...

  8. hibernate.cfg.xml

    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN&q ...

  9. 【转译】加入ZigBee联盟,共画物联网的未来

    Zigbee联盟是物联网全球革命的领导者,创造了随心控制的时代.Zigbee简化了无线产品的整合.加快了市场化,同时让那些希望引入高能效的无线控制的厂商,降低了成本和风险.我们的联盟成员在开放的全球环 ...

  10. 【挖财工作笔记】idea使用指南

    一 安装破解 破解选择服务器,然后选择地址:http://www.iteblog.com/idea/key.php  http://idea.iteblog.com/key.php  http://i ...