Created
June 15, 2018 18:09
-
-
Save maxistar/d1bf43ce899c34537e7c3db58f10c01e to your computer and use it in GitHub Desktop.
getMaxProfit function
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
/** | |
* 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