Created
April 20, 2016 20:50
-
-
Save gpfiel/fd29294bcef2070b350f4aa6bf4f7a9a to your computer and use it in GitHub Desktop.
Question1Toptal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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