法1:贪心,最佳解法
class Solution {
public int maxProfit(int[] prices) {
int ans = 0;
if (prices.length < 2) {
return 0;
}
for (int i = 1; i < prices.length; ++i) {
ans += Math.max(prices[i] - prices[i - 1], 0);
}
return ans;
}
}
股票问题总结
六种股票问题总结https://blog.youkuaiyun.com/weixin_47692079/article/details/117202705
本文介绍了如何使用贪心策略解决股票交易中的最大利润问题,通过Solution类中的maxProfit方法,计算给定价格数组中买入并持有股票后可能获得的最大收益。
338

被折叠的 条评论
为什么被折叠?



