Sunday, August 12, 2018

122. Best Time to Buy and Sell Stock II


You may complete as many transactions as you like
So the result is the sum of all profitable transactions.
99.90 %
class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        int sum = 0;
        int diff = 0;
        for (int i = 0; i < prices.length - 1; i++) {
            diff = prices[i + 1] - prices[i];
            if (diff > 0) {
                sum += diff;
            }
        }
        return sum;
    }
}

No comments:

Post a Comment