C++ solution beats 93.15%


  • 0
    F

    Basic idea is same as other solutions: total area minus overlapped area. The private method calculate overlapped length for width and height.

    class Solution {
    public:
        int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
            return abs(C - A) * abs(D - B) + abs(G - E) * abs(H - F) - getOverlappedLen(A, C, E, G) * getOverlappedLen(B, D, F, H);
        }
    private:
        int getOverlappedLen(int low1, int high1, int low2, int high2)
        {
            if (low2 > high1 || low1 > high2)
            {
                return 0;
            }
            return min(high2, high1) - max(low2, low1);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.