Java easy solution


  • 2
    R
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
      int xOverlapping = calcOverlap(A, C, E, G);
      int yOverlapping = calcOverlap(B, D, F, H);
      return (C - A) * (D - B) + (G - E) * (H - F) - xOverlapping * yOverlapping;
    }
    
    private int calcOverlap(long a1, long a2, long b1, long b2) {
      long diff = (Math.min(a2, b2) - Math.max(a1, b1));
      if (diff > 0) {
        return (int) diff;
      } else {
        return 0;
      }
    }

Log in to reply
 

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