Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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;
- }
Add Comment
Please, Sign In to add comment