122. The best time to Buy and sell stocks II

class Solution {
    public int maxProfit(int[] prices) {
        int res = 0;
        if(prices==null || prices.length==0){
            return 0;
        }
        int len =prices.length;

        int high = prices[0];
        int low = prices[0];
        int i=0;
        while(I <len-1){// calculate buywhile(i<len-1 && prices[i]>=prices[i+1]){ i++; } low =prices[i]; // Calculate sellwhile(i<len-1 && prices[i]<=prices[i+1]){ i++; } high =prices[i]; Res += high-low; }returnres; }}Copy the code