Skip to content

Instantly share code, notes, and snippets.

@maxistar
Created June 15, 2018 18:09
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 maxistar/d1bf43ce899c34537e7c3db58f10c01e to your computer and use it in GitHub Desktop.
Save maxistar/d1bf43ce899c34537e7c3db58f10c01e to your computer and use it in GitHub Desktop.
getMaxProfit function
/**
* Returns of max profit for the given market values
* it there are not possible to compleat the deal it will return 0
*
* @param $marketValues the array wit market values, should contain at least one value
* @return int
*/
function getMaxProfit($marketValues) : int
{
$minValue = $marketValues[array_keys($marketValues)[0]];
$maxProfit = 0;
foreach($marketValues as $day => $value) {
if ($minValue > $value) {
$minValue = $value;
}
else if ($value - $minValue > $maxProfit) {
$maxProfit = $value - $minValue;
}
}
return $maxProfit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment