leetcode python best-time-to-buy-and-sell-stock(包含123)

best-time-to-buy-and-sell-stock(后面还有ii和iii)

Say you have an array for which the i th element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),
design an algorithm to find the maximum profit.

题意

假设你有一个数组,里面存放的第i个元素表示第i天的股票的价格,如果你最多只允许进行一次交易(买进和卖出股票视为一次交易)
请设计一个算法得到最大利润。

思路

假设在某天卖出,只需知道这天之前最小的买入价格,设一个变量low记录就成

python实现

class Solution:
    def maxProfit(self, prices):
        if not prices:
            return 0
        low = prices[0]
        maxprofit = prices[1] - prices[0]
        for i in range(2, len(prices)):
            low = min(low, prices[i - 1])
            maxprofit = max(maxprofit, prices[i] - low)
        return maxprofit


if __name__ == '__main__':
    a = [2, 3, 1, 2, 6, 5]
    so = Solution()
    print(so.maxProfit(a))

best-time-to-buy-and-sell-stock ii

Say you have an array for which the i th element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you
like (ie, buy one and sell one share of the stock multiple times). However, you may not engage
in multiple transactions at the same time (ie, you must sell the stock before you buy again).

题意

允许进行多次交易,即可以多次买入和卖出,但手中最多只能持有一支股票,在再次买入的时候必须将之前的股票卖出,求能获取的最大利润。

思路

一旦后一天比前一天大,就卖出,代码实际上是变相的统计了所有递增序列的末项-首项

python实现

class Solution:
    def maxProfit(self, prices):
        if not prices:
            return 0
        maxprofit = 0
        for i in range(1, len(prices)):
            if prices[i] > prices[i - 1]:
                maxprofit += prices[i] - prices[i - 1]
        return maxprofit


if __name__ == '__main__':
    a = [2, 3, 1, 2, 6, 5]
    so = Solution()
    print(so.maxProfit(a))

best-time-to-buy-and-sell-stock iii

Say you have an array for which the i th element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

题意

允许进行二次交易,即可以二次买入和卖出,但手中最多只能持有一支股票,在再次买入的时候必须将之前的股票卖出,求能获取的最大利润。

思路

和第一题很相似,但是要分为两段来看

python实现

class Solution():
    def maxProfit(self, prices):
        if not prices:
            return 0
        n = len(prices)
        # pre_profit[i]表示i天之前的最大利润
        pre_max_profit = [0] * n
        # pro_profit[i]表示i天之后的最大利润
        pro_max_profit = [0] * n
        max_profit = 0

        low = prices[0]
        for i in range(1, n):
            low = min(prices[i], low)
            pre_max_profit[i] = max(pre_max_profit[i - 1], prices[i] - low)

        # 求i天之后的最大利润,从后面找到最大的利润,再减去当天的
        high = prices[n - 1]
        for k in range(n - 2, -1, -1):
            high = max(high, prices[k])
            pro_max_profit[k] = max(pro_max_profit[k + 1], high - prices[k])
        print(pro_max_profit)

        for j in range(n):
            max_profit = max(max_profit, pre_max_profit[j] + pro_max_profit[j])
        return max_profit


if __name__ == '__main__':
    a = [2, 3, 1, 2, 6, 5]
    so = Solution()
    print(so.maxProfit(a))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值