public class Solution {
public int MaxProfit(int[] prices) {
if(prices.Length < 2){
return 0;
}
var profit = new int[prices.Length];
profit[0] = 0;
for(var i = 1;i < prices.Length; i++){
var delta = prices[i] - prices[i-1];
if(delta > 0){
profit[i] = profit[i-1] + delta;
}else{
profit[i] = delta + profit[i-1] > 0 ? delta + profit[i-1] : 0;
}
}
return profit.Max();
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode -- Best Time to Buy and Sell Stock
原文:http://blog.csdn.net/lan_liang/article/details/49188129