2015年6月7日星期日

LeetCode: Rectangle Area

Difficulty: how to get dist_w/h.
At first I just got this distance with a difference from points ==!

dist_w/h ==> the distance between two farthest horizontal/vertical points of two rectangles
rep_w/h ==>repetition distance of width and height

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int width1=abs(A-C);
        int width2=abs(E-G);
        int height1=abs(B-D);
        int height2=abs(F-H);
        
        int dist_w= ((C>G)?C:G) -  ((A<E)?A:E) ;
        int dist_h= ((D>H)?D:H) - ((B<F)?B:F);

        int rep_w=abs(width1+width2-dist_w);
        int rep_h=abs(height1+height2-dist_h);
        int area_sum=width1*height1+width2*height2;
        if(dist_w<(width1+width2) && dist_h<(height1+height2))
            return area_sum-rep_w*rep_h;
        return area_sum;
    }
};

没有评论:

发表评论