[Leetcode]Rectangle Area

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

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;
    }
};

posted on 2015-12-14 20:16  泉山绿树  阅读(24)  评论(0)    收藏  举报

导航