本文主要是介绍力扣HOT100 - 121. 买卖股票的最佳时机,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
解题思路:
每次遍历price,更新最小的cost和最大的profit
class Solution {public int maxProfit(int[] prices) {int cost = Integer.MAX_VALUE, profit = 0;for (int price : prices) {cost = Math.min(cost, price);profit = Math.max(profit, price - cost);}return profit;}
}
这篇关于力扣HOT100 - 121. 买卖股票的最佳时机的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!