Last active
May 25, 2016 18:55
-
-
Save juliobetta/8e1cfba2872b20b23c9f9d32642f6ac7 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Total stops | |
* Execute the code at http://sandbox.onlinephpfunctions.com/code/98e0909b60bc2f1096961cc2a7be39efd7cab6cb | |
* @param array $A people weights | |
* @param array $B people destination floor | |
* @param int $M total floors | |
* @param int $X elevator's people capacity | |
* @param int $Y elevator's weight capacity | |
* @return int total stops | |
*/ | |
function solution($A, $B, $M, $X, $Y ) { | |
$stops = 0; | |
$weight = 0; | |
$total_people = 0; | |
$elevator_travels = []; | |
$travel_index = 0; | |
for($i = 0; $i < sizeof($A); $i++) { | |
if($B[$i] > $M) { | |
continue; | |
} | |
$total_people++; | |
$weight += $A[$i]; | |
if($total_people > $X || $weight > $Y) { | |
$travel_index++; | |
$weight = 0; | |
$total_people = 0; | |
} | |
$elevator_travels[$travel_index][$B[$i]] = 'visited'; // do not repeat floors | |
} | |
$stops += sizeof($elevator_travels); // total of returns to ground | |
$stops += array_sum(array_map('sizeof', $elevator_travels)); // total of travels | |
return $stops; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment