Say you have an array for which the i_th element is the price of a given stock on day _i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note:You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

只要可以低买高卖,都进行交易

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

        int res = 0;
        for(int i = 0; i < prices.length - 1; i++) {
            res += Math.max(0, prices[i + 1] - prices[i]);
        }

        return res;
    }
}

results matching ""

    No results matching ""