Sharing of my 0ms AC Solution.


  • -1
    R
    class Solution {
        public:
    
            struct Point
            {
                Point(int x,int y):_x(x),_y(y){}
                bool operator>(const Point& other) {
                    return _x>other._x;
                }
                Point()
                {}
                int _x,_y;
            };
            struct Rect
            {
                float getArea() {
                    return abs(m_tr._y-m_bl._y)*abs(m_tr._x-m_bl._x);
                }
                Point& getBottomLeft() {
                    return m_bl;
                }
                Point& getTopRight() {
                    return m_tr;
                }
                float isOutsideOf(Rect other) {
                    bool isOutside=getBottomLeft()._y>=other.getTopRight()._y ||getTopRight()._y<=other.getBottomLeft()._y 
                        ||getTopRight()._x<=other.getBottomLeft()._x ||getBottomLeft()._x>=other.getTopRight()._x;
                    return isOutside;
                }
                Rect(Point bl,Point tr) {
                    m_bl=bl; 
                    m_tr=tr;
                    check();
                }
                Rect(int A,int B,int C,int D) 
                {
                    m_bl=Point(A,B);
                    m_tr=Point(C,D);
                    check();
                }
                Rect() {}
                void check()
                {
                    if(m_bl>m_tr)
                    {
                        Point temp=m_bl;
                        m_bl=m_tr;
                        m_tr=temp;
                    }
                }
                Point m_bl,m_tr;
            };
            int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) 
            {
                Rect left(A,B,C,D);
                Rect right(E,F,G,H);
                if(right.isOutsideOf(left))
                    return left.getArea()+right.getArea();
                Rect overlap;
                //set tl._x
                overlap.getBottomLeft()._x=max(left.getBottomLeft()._x,right.getBottomLeft()._x);
                //now calc tl._y
                overlap.getBottomLeft()._y=max(left.getBottomLeft()._y,right.getBottomLeft()._y);
                //now calc br._x
                overlap.getTopRight()._x=min(left.getTopRight()._x,right.getTopRight()._x);
                //now calc br._y
                overlap.getTopRight()._y=min(left.getTopRight()._y,right.getTopRight()._y);
                return left.getArea()+right.getArea()-overlap.getArea();
            }
    };

Log in to reply
 

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