买卖股票的最佳时机

题目描述:给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。

你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。

返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。

示例说明请见LeetCode官网。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:动态规划

如果prices为空或者prices只有一个值,直接返回0;

否则,声明一个变量result为0,buyPrice为prices的第一个值,从prices的第2个值开始遍历:

  • 如果当前值等于buyPrice,则跳过处理下一个值;
  • 如果当前值小于buyPrice,则更新buyPrice为当前值;
  • 如果当前值大于buyPrice,判断当前值和buyPrice的差值是否比result大,如果更大,则更新result的值。

遍历完成后,返回result。

  1. public class LeetCode_121 {
  2. public static int maxProfit(int[] prices) {
  3. if (prices == null || prices.length < 2) {
  4. return 0;
  5. }
  6. int result = 0;
  7. int buyPrice = prices[0];
  8. for (int i = 1; i < prices.length; i++) {
  9. if (prices[i] == buyPrice) {
  10. continue;
  11. } else if (prices[i] < buyPrice) {
  12. buyPrice = prices[i];
  13. } else {
  14. if (prices[i] - buyPrice > result) {
  15. result = prices[i] - buyPrice;
  16. }
  17. }
  18. }
  19. return result;
  20. }
  21. public static void main(String[] args) {
  22. int[] prices = new int[]{7, 1, 5, 3, 6, 4};
  23. System.out.println(maxProfit(prices));
  24. }
  25. }

【每日寄语】 三月悄然去,四月翩然来。最美四月天,莫负春光美。前路皆坦荡,万事皆可期。