-
-
Save zhaofenghao/54a6188c1e88c986d7abc8be6b13e17a 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
public function getBiggestSubarray($arr, $max, $record = []) | |
{ | |
if (count($arr) <= 1) { | |
return $arr[0] > $max ? $arr : $record; | |
} | |
foreach ($arr as $k => $v) { | |
$sum = array_sum(array_slice($arr, $k)); | |
if ($sum >= $max) { | |
$max = $sum; | |
$record = array_slice($arr, $k); | |
} | |
} | |
$arr = array_splice($arr, 0, count($arr) - 1); | |
return $this->getBiggestSubarray($arr, $max, $record); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment