122. Best Time to Buy and Sell Stock II

QUESTION:

Say you have an array for which the ith 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 (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

EXPLANATION:

其实就是贪心算法,但是贪心算法是需要证明其中的正确性的。

做了这些算法之后发现,其实很多的算法问题只是数学问题,如果数学能够学的好的话,这些应该还是可以简单的。

SOLUTION:

public int maxProfit(int[] prices) {
        int result = 0;
        if (prices == null || prices.length == 1) return result;
        for (int i = 1; i < prices.length; i++) {
            result = prices[i]-prices[i-1] >0?result+prices[i]-prices[i-1]:result;
        }
        return result;
    }
>