Created
June 11, 2013 06:09
-
-
Save jayzeng/5754772 to your computer and use it in GitHub Desktop.
Find max
This file contains hidden or 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 findMax(array $input, $range) { | |
$curSum = 0; | |
for($i=0;$i<count($input);$i++) { | |
$subArr = array_slice($input, $i, $range); | |
$sum = 0; | |
foreach($subArr as $arr) { | |
$sum += $arr; | |
} | |
if($curSum < $sum) { | |
$curSum = $sum; | |
} | |
} | |
return $curSum; | |
} | |
$input = array(1,2,1,3,4,5); | |
var_dump(findMax($input,2)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment