Tuesday 4 October 2016

Rectangle Area (LeetCode)

Problem: Find the total area covered by two rectilinear rectangles in a 2D plane.

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int twoAreas = (C-A)*(D-B) + (G-E)*(H-F);
        //for common parts
        int lowx=max(A, E), hix=min(C, G), lowy=max(B, F), hiy=min(D, H);
        int commonArea = 0;
        if(lowx<hix && lowy<hiy)
            twoAreas -= (hix-lowx)*(hiy-lowy);
        return twoAreas;
    }
};

No comments:

Post a Comment