package leetcode;

/**
 * https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
 */
public class Problem122 {
    public int maxProfit(int[] prices) {
        return 0;
    }
    
    public static void main(String[] args) {
        Problem122 prob = new Problem122();
        System.out.println(prob.maxProfit(new int[]{1, 2, 4, 2, 1, 3}));
        System.out.println(prob.maxProfit(new int[]{3, 2, 6, 5, 0, 3}));
        System.out.println(prob.maxProfit(new int[]{3, 1, 2, 6, 1, 6, 8}));
        
    }
}

@@ -168,70 +168,183 @@
     
-return 0;%0A    %7D
+boolean buy = true;%0A        int profit = 0;
 %0A    
-%0A
     
-public static void main(String%5B%5D args
+int buyPrice = Integer.MAX_VALUE;%0A        int sellPrice = Integer.MIN_VALUE;%0A        for (int i = 0; i %3C prices.length; i++
 ) %7B%0A
@@ -355,42 +355,141 @@
     
+  
-Problem122 prob = new Problem122()
+  if (buy) %7B%0A                if (buyPrice %3C prices%5Bi%5D) %7B%0A                    sellPrice = prices%5Bi%5D;%0A                    buy = false
 ;%0A  
@@ -498,71 +498,209 @@
     
+ 
-System.out.println(prob.maxProfit(new int%5B%5D%7B1, 2, 4, 2, 1, 3%7D))
+       %7D else %7B%0A                    buyPrice = prices%5Bi%5D;%0A                %7D%0A            %7D else %7B // sell%0A                if (sellPrice %3E prices%5Bi%5D) %7B%0A                    profit += sellPrice - buyPrice
 ;%0A  
@@ -709,157 +709,347 @@
     
+ 
-System.out.println(prob.maxProfit(new int%5B%5D%7B3, 2, 6, 5, 0, 3%7D));%0A        System.out.println(prob.maxProfit(new int%5B%5D%7B3, 1, 2, 6, 1, 6, 8%7D));%0A        
+           buy = true;%0A                    buyPrice = prices%5Bi%5D;%0A                    sellPrice = Integer.MIN_VALUE;%0A                %7D else %7B%0A                    sellPrice = prices%5Bi%5D;%0A                %7D%0A            %7D%0A        %7D%0A        if (buyPrice %3C sellPrice) %7B%0A            profit += sellPrice - buyPrice;%0A        %7D%0A        return profit;
 %0A   

Solve problem 122