您的位置:首页 > 其它

[Leetcode]Rectangle Area

2015-12-14 20:16 295 查看
Rectangle Area

Total Accepted: 24959 Total Submissions: 89043 Difficulty: Easy

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.

Rectangle Area

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.

Subscribe to see which companies asked this question

[code]class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        unsigned int totalArea = (C - A) * (D - B) + (G - E) * (H - F);
        unsigned long long hBig = max(D,H) - min(B,F);
        unsigned long long lBig = max(G,C) - min(A,E);
        if(hBig >= ((D - B) + (H - F))  || lBig >= ((C - A) + (G - E)))
            return totalArea;
        unsigned long long h = ((D - B) + (H - F)) - hBig;
        unsigned long long l = ((C - A) + (G - E)) - lBig;
        return totalArea - (unsigned long long)h * (unsigned long long)l;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: