题目描述
假设你有一个数组,其中第i个元素表示某只股票在第i天的价格。
设计一个算法来寻找最大的利润。你可以完成任意数量的交易(例如,多次购买和出售股票的一股)。但是,你不能同时进行多个交易(即,你必须在再次购买之前卖出之前买的股票)。
public int maxProfit (int[] prices) {
// write code here
if(prices.length==0) return 0;
int maxpro = 0;
for(int i=0;i<prices.length-1;i++){
if(prices[i]<=prices[i+1]){
maxpro += prices[i+1]-prices[i];
}
}
return maxpro;
}