Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static getMaxProfit(List<double> stockPrices) {
- int size = stockPrices.size();
- double localMin = stockPrices.indexOf(0), localMax;
- double profit = 0, buy = 0;
- boolean transactionPhase = true;
- for(int i = 1; i < size; i++) {
- if(transactionPhase) {
- if (localMin > stockPrices.indexOf(i)) {
- localMin = stockPrices.indexOf(i);
- } else if (localMin < stockPrices.indexOf(i)) {
- buy = localMin;
- transactionPhase = false;
- localMax = stockPrices.indexOf(i);
- }
- } else {
- if (localMax < stockPrices.indexOf(i)) {
- localMax = stockPrices.indexOf(i);
- if(i == size -1) {
- profit = profit + (localMax - buy);
- }
- } else if ((localMax > stockPrices.indexOf(i))) {
- profit = profit + (localMax - buy);
- transactionPhase = true;
- buy = stockPrices.indexOf(i);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement