LeetCode2

##122. Best Time to Buy and Sell Stock II
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).

题意:设计一个最大收益的算法,可以进行多次交易(但是必须先买入才能卖出)。

int prices[]

int days[]

要想收益最大,低价买入,高价卖出。

代码很简单:主要是思想,问题是求最大和,实际上使计算上升阶段的所有值得和

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