5ms java in a readable


  • 0
    S
    public class Solution {
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        
        int areaA = (C-A)*(D-B); 
        int areaB = (G-E)*(H-F); 
        int overlapAB = overlapArea(A,B,C,D,E,F,G,H); 
        
        int area = areaA + areaB - overlapAB; 
        return area; 
    }
    
    private int overlapArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int bottomX = Math.max(A, E); 
        int bottomY = Math.max(B, F);
        int topX = Math.min(C,G);
        int topY = Math.min(D,H);
        
        int overlap = 0; 
        if(topX > bottomX && topY > bottomY) { 
            overlap = (topX - bottomX) * (topY - bottomY);   
        } 
        return overlap; 
    }
    

    }


Log in to reply
 

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