zoukankan      html  css  js  c++  java
  • [LeetCode] 121. 买卖股票的最佳时机

    题目链接 : https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/

    题目描述:

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

    如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。

    注意你不能在买入股票前卖出股票。

    示例:

    示例 1:

    输入: [7,1,5,3,6,4]
    输出: 5
    解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
         注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。
    

    示例 2:

    输入: [7,6,4,3,1]
    输出: 0
    解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
    

    思路:

    思路一: Kadane's Algorithm参考维基百科

    简单介绍一下,如果一个数组为[a1, a2, a3, a4, a5],

    a5 - a1 = (a2 - a1) + (a3 - a2) + (a4 - a3) + (a5 - a4)

    所以,我们这一题就是找两个差值最大数!

    class Solution:
        def maxProfit(self, prices: List[int]) -> int:
            res = 0
            cur_max = 0
            for i in range(1, len(prices)):
                # 记录目前位置以i为结束的, 差值最大值
                cur_max += (prices[i] - prices[i-1])
                # 如果 cur_max 小于0 说明要改变起始的位置
                cur_max = max(0, cur_max)
                res = max(res, cur_max)
            return res
    

    java

    class Solution {
        public int maxProfit(int[] prices) {
            if (prices == null || prices.length == 0) return 0;
            int cur_max = 0;
            int res = 0;
            for (int i = 1; i < prices.length; i++) {
                cur_max += prices[i] - prices[i - 1];
                cur_max = Math.max(0, cur_max);
                res = Math.max(cur_max, res);
            }
            return res;
        }
    }
    

    思路二 : 动态规划,我们可以遍历数组,记录前面最小的价格,用当天价格减去最小价格,一定是这天可以获得最大利润!

    def maxProfit(self, prices: List[int]) -> int:
            if not prices: return 0
            res = 0
            cur_min = prices[0]
            for i in range(1, len(prices)):
                res = max(res, prices[i] - cur_min)
                cur_min = min(cur_min, prices[i])
            return res
    

    java

    class Solution {
        public int maxProfit(int[] prices) {
            if (prices == null || prices.length == 0) return 0;
            int res = 0;
            int pre_min = prices[0];
            for (int i = 1; i < prices.length; i++){
                res = Math.max(res, prices[i] - pre_min);
                pre_min = Math.min(pre_min, prices[i]);
            }
            return res;  
        }
    }
    
  • 相关阅读:
    ADO.NET Entity Framework之ObjectQuery
    高效byte[]和string互转的方法
    异步邮件发送
    ADO.NET Entity Framework之ObjectContext
    【转】Memcache and Mongodb
    一周工作总结一次SQL优化记录
    Ubuntu下有关Java和数据库的一些工作记录(一)
    自定义函数,替换字符串中指定位置的字符
    一次优化记录
    对比shrink和move
  • 原文地址:https://www.cnblogs.com/powercai/p/11172057.html
Copyright © 2011-2022 走看看