Skip to content

Instantly share code, notes, and snippets.

@gpfiel
Created April 20, 2016 20:50
Show Gist options
  • Save gpfiel/fd29294bcef2070b350f4aa6bf4f7a9a to your computer and use it in GitHub Desktop.
Save gpfiel/fd29294bcef2070b350f4aa6bf4f7a9a to your computer and use it in GitHub Desktop.
Question1Toptal
<?php
function solution($K, $L, $M, $N, $P, $Q, $R, $S) {
//get inputs to check intersection
$leftSide = max($K, $P);
$rightSide = min($M, $R);
$bottomSide = max($L, $Q);
$topSide = min($N, $S);
//the area of the sum of the rectangles
$sum = (($M - $K) * ($N - $L)) + (($R - $P) * ($S - $Q));
//case the rectangles interserc should remove it
if ($leftSide < $rightSide && $bottomSide < $topSide) {
$intersection = ($rightSide - $leftSide) * ($topSide - $bottomSide);
$sum = $sum - $intersection;
}
//sum exceeds
if ($sum >= 2147483647) {
return -1;
}
return $sum;
}
echo solution(-4, 1, 2, 6, 0, -1, 4, 3);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment