Easy python code


  • 1
    B
    class Solution:
    # @param {integer} A
    # @param {integer} B
    # @param {integer} C
    # @param {integer} D
    # @param {integer} E
    # @param {integer} F
    # @param {integer} G
    # @param {integer} H
    # @return {integer}
    def computeArea(self, A, B, C, D, E, F, G, H):
        S=(C-A)*(D-B) + (G-E)*(H-F)
        if A>G or C<E or D<F or B>H :
            return S
        else:
            s_common=(min(C,G)-max(A,E))*(min(D,H)-max(B,F))
            return S-s_common

  • 0
    A

    why it can solve it


  • 0
    B

    if two rectangles don't overlap, just return the sum of the two area,
    otherwise compute the overlapping and Subtract that from the sum.


  • 0
    F

    uh...So the question assume (A,B) and (E,F) must be the bottom left? I thought they would be randomly placed...what if it is in this case?


  • 0
    B

    I think yes, even if they are randomly placed,the two point of the rectangle can't be the same edge, so at most 3 case need to consider.


  • 0
    C

    Actually It seems you can omit some lines:

    def computeArea(self, A, B, C, D, E, F, G, H):
        overlap = max(0, min(C, G)-max(A, E)) * max(0, min(D, H)-max(B, F))
        return (C-A)*(D-B)+(G-E)*(H-F)-overlap

Log in to reply
 

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