Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. public static getMaxProfit(List<double> stockPrices) {
  2. int size = stockPrices.size();
  3. double localMin = stockPrices.indexOf(0), localMax;
  4. double profit = 0, buy = 0;
  5. boolean transactionPhase = true;
  6. for(int i = 1; i < size; i++) {
  7. if(transactionPhase) {
  8. if (localMin > stockPrices.indexOf(i)) {
  9. localMin = stockPrices.indexOf(i);
  10. } else if (localMin < stockPrices.indexOf(i)) {
  11. buy = localMin;
  12. transactionPhase = false;
  13. localMax = stockPrices.indexOf(i);
  14. }
  15. } else {
  16. if (localMax < stockPrices.indexOf(i)) {
  17. localMax = stockPrices.indexOf(i);
  18. if(i == size -1) {
  19. profit = profit + (localMax - buy);
  20. }
  21. } else if ((localMax > stockPrices.indexOf(i))) {
  22. profit = profit + (localMax - buy);
  23. transactionPhase = true;
  24. buy = stockPrices.indexOf(i);
  25.  
  26. }
  27. }
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement