Guest User

Untitled

a guest
Jun 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. /**
  2. * Returns of max profit for the given market values
  3. * it there are not possible to compleat the deal it will return 0
  4. *
  5. * @param $marketValues the array wit market values, should contain at least one value
  6. * @return int
  7. */
  8. function getMaxProfit($marketValues) : int
  9. {
  10. $minValue = $marketValues[array_keys($marketValues)[0]];
  11. $maxProfit = 0;
  12.  
  13. foreach($marketValues as $day => $value) {
  14. if ($minValue > $value) {
  15. $minValue = $value;
  16. }
  17. else if ($value - $minValue > $maxProfit) {
  18. $maxProfit = $value - $minValue;
  19. }
  20. }
  21. return $maxProfit;
  22. }
Add Comment
Please, Sign In to add comment