Skip to content

Instantly share code, notes, and snippets.

@crised
Created December 4, 2015 13:06
Show Gist options
  • Save crised/24d359791edc94e074c1 to your computer and use it in GitHub Desktop.
Save crised/24d359791edc94e074c1 to your computer and use it in GitHub Desktop.
/**
* Created by crised on 09-11-15.
*/
public class Solution {
public Solution() {
int K = -4, L = 1, M = 2, N = 6, P = 0, Q = -1, R = 4, S = 3;
System.out.println(solution(K, L, M, N, P, Q, R, S));
}
public int solution(int K, int L, int M, int N, int P, int Q, int R, int S) {
//x,y sides of intersected rectangle
int x = Math.min(M, R) - Math.max(K, P);
int y = Math.min(N, S) - Math.max(L, Q);
long inter = 0;
if (x >= 0 && y >= 0) inter = x * y;
long A1 = Math.abs((M - K) * (N - L));
long A2 = Math.abs((R - P) * (S - Q));
long sol = A1 + A2 - inter;
if (sol <= Integer.MAX_VALUE) return (int) sol;
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment