您的位置:首页 > 其它

leetcode 121. Best Time to Buy and Sell Stock-股市交易

2016-04-10 21:41 225 查看
原题链接:121.Best Time to Buy and Sell Stock
【思路】

本题考查动态规划。复杂度为n(O),在遍历过程中记录最小值min。结果 result 只取决于 result 与 prices[i] - min 中的较大值:

public int maxProfit(int[] prices) {
int result = 0, min = 0x7fff_ffff;
for (int price : prices)
if (price < min) min = price;
else result = Math.max(result, price - min);
return result;
}
198 / 198 test
cases passed. Runtime: 2
ms Your runtime beats 60.20% of javasubmissions.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: