资讯专栏INFORMATION COLUMN

[LeetCode] 223. Rectangle Area

JayChen / 2888人阅读

Problem

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.

Solution
class Solution {
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int a1 = (C-A)*(D-B);
        int a2 = (G-E)*(H-F);
        int left = Math.max(A,E);
        int right = Math.min(C,G);
        int top = Math.min(D,H);
        int bottom = Math.max(B,F);
        if (left < right && bottom < top) return a1+a2-(right-left)*(top-bottom);
        else return a1+a2;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73014.html

相关文章

  • leetcode363. Max Sum of Rectangle No Larger Than K

    摘要:思路一暴力循环如果我们将矩阵中的每个子矩阵都枚举出来,并计算其元素和,从而得出小于的最大值即可。 题目要求 Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k. E...

    nemo 评论0 收藏0
  • [Leetcode] Rectangle Area 矩形面积

    摘要:数学法复杂度时间空间思路基本的数学题,考察的是我们能否全面的考虑到所有可能。如果两个矩形没有重叠部分,则直接计算两个矩形面积之和就行了。因为两个矩形的坐标都可能比对方小,所以我们一共有四种可能情况是不重叠的。 Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. ...

    Cheng_Gang 评论0 收藏0
  • [LeetCode/LintCode] Construct the Rectangle

    Problem For a web developer, it is very important to know how to design a web pages size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose l...

    sf_wangchong 评论0 收藏0
  • [LeetCode] 84. Largest Rectangle in Histogram

    Problem Given n non-negative integers representing the histograms bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. showImg(https://segmentfault.com/img...

    BaronZhang 评论0 收藏0
  • [Leetcode] Largest Rectangle (in Histogram) 最大矩形

    摘要:以此类推,如果一直到栈为空时,说明刚出来的竖条之前的所有竖条都比它自己高,不然不可能栈为空,那我们以左边全部的宽度作为长方形的宽度。 Largest Rectangle in Histogram Given n non-negative integers representing the histograms bar height where the width of each bar...

    邹强 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<