1 line Java solution


  • 0
    C
    public class Solution {
    	public int computeArea(int A, int B, int C, int D, int E, int F, int G,int H) {
    		return ((C - A) * (D - B) + (G - E) * (H - F)) - ((B >= H || D <= F || C <= E || A >= G) ? ((E > A ? E : A) - (G > C ? C: G)) * ((F > B ? F : B) - (H > D ? D : H)): 0);
    	}
    }

  • 0
    B

    How does this work? Can you explain it?


  • 1
    C

    In face, there are two kinds of situations:

    1. there is a coverd area or not, which can be decided easily
      the condition is follow, in which A>=G means the minimum left x of one is larger than the maximum right x of the other, so they will not cross. And the other conditions are the same.
      B >= H || D <= F || C <= E || A >= G

    2. if there are covered area, it will be easily to calculate the area as follow rule
      In order to calculate the left corner(x,y) of the covered area, (x,y) will be the bigger one of the left corner of the two rectangle
      (x,y) = (E > A ? E : A, F > B ? F : B)
      In order to calculate the right corner(x1,y1) of the covered area,(x1,y1) will be the smaller one of the right corner of the two rectangle
      (x1,y1) = (G > C ? C : G, H > D ? D : H)

    3. the final area = the area of the two rectangle - covered area


  • 0
    D

    your solution is wrong, the method of get weather there is a coverd area is wrong.


Log in to reply
 

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