One step math in JAVA


  • 3
    B
    public class Solution {
        public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
            int areaA = (D - B) * (C - A);
            int areaB = (H - F) * (G - E);
            int lowerTop = Math.min(D, H);
            int upperBot = Math.max(B, F);
            int lefterRight = Math.min(C, G);
            int righterLeft = Math.max(A, E);
            int overlap = 0;
            if (lowerTop > upperBot && lefterRight > righterLeft) {
                overlap = (lowerTop - upperBot) * (lefterRight - righterLeft);
            }
            return areaA + areaB - overlap;
        }
    }
    

  • 0
    C

    Nicely explained, it was bit difficult for me to visualize this problem before I saw your solution


Log in to reply
 

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