Advertisement
macferreira

test_elevator

Oct 13th, 2015
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.14 KB | None | 0 0
  1. function solution($A, $B, $M, $X, $Y) {
  2.     // write your code in PHP5.5
  3.    
  4.     $capacityTempControl = 1;
  5.     $weightTempControl = 0;
  6.     $elevatorMaxWeight = $Y;
  7.     $elevatorCapacity = $X;
  8.    
  9.     $rideControl = 1;
  10.     $rides = array();
  11.    
  12.     for ($i = 0; $i <= (count($A)-1); $i++) {
  13.        
  14.         if($capacityTempControl <= $elevatorCapacity && ($weightTempControl+$A[$i]) <= $elevatorMaxWeight) {
  15.             $rides[$rideControl][] = $B[$i];
  16.             $weightTempControl+=$A[$i];
  17.             $capacityTempControl++;        
  18.         } else {
  19.             $rideControl++;
  20.             $rides[$rideControl][] = $B[$i];
  21.             $capacityTempControl = 2;
  22.             $weightTempControl = $A[$i];
  23.         }                
  24.     }
  25.    
  26.     $stopCountVar = 0;
  27.     $lastStopTempCount = -1;
  28.    
  29.     foreach ($rides as $value) {
  30.         foreach ($value as $finalVal) {
  31.             if($lastStopTempCount != $finalVal) {
  32.                 $lastStopTempCount = $finalVal;
  33.                 $stopCountVar++;
  34.             }
  35.         }
  36.         $stopCountVar++;
  37.         $lastStopTempCount = -1;
  38.     }
  39.    
  40.     return($stopCountVar);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement