Skip to content

Instantly share code, notes, and snippets.

@zhaofenghao
Created September 16, 2020 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhaofenghao/54a6188c1e88c986d7abc8be6b13e17a to your computer and use it in GitHub Desktop.
Save zhaofenghao/54a6188c1e88c986d7abc8be6b13e17a to your computer and use it in GitHub Desktop.
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