您的位置:首页 > 其它

121. Best Time to Buy and Sell Stock

2016-06-15 05:45 295 查看
顺着数组走,保存:

1.到目前为止最大profit

2.到目前为止最小price

更新两个数据,结尾返回maxProfit

public int maxProfit(int[] prices) {
if(prices.length < 1) {
return 0;
}
int minPrice = prices[0];
int maxProfit = 0;
for(int i = 1; i < prices.length; i++) {
minPrice = (prices[i] < minPrice)? prices[i]: minPrice;
maxProfit = (maxProfit < prices[i] - minPrice)? prices[i] - minPrice: maxProfit;
}
return maxProfit;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: