python 实现买卖股票的最佳时机简单到困难

给定一个整数数组prices,其中prices[i]是一只给定股票第i天的价格。

设计一个算法来计算你所能获得的最大利润,你可以尽可能地完成最多交易(多次买入同一支股票)。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)

示例:

输入:prices=[7, 1, 5, 3, 6, 4]

输出:7

解释:

在第2天(股票价格=1)的时候买入,在第3天(股票价格=5)的时候卖出,这笔交易所获得利润=5-1

在第4天(股票价格=3)的时候买入,在第5天(股票价格=6)的时候卖出,这笔交易所获得利润=6-3

class Soulution(object):
    def maxProfit(self, prices):
        """
        :type pricesL:   list[int]
        :return:
        """
        hold = 0
        pric = []
        temp = []
        flag = 0
        msum = 0
        if len(prices) <= 2:
            if not prices:
                return 0
            if len(prices) == 1:
                return 0
            if prices[0] > prices[1]:
                return 0
            if prices[0] < prices[1]:
                return prices[1] - prices[0]
        for i in range(len(prices) - 1):
            if prices[i + 1] > prices[i] and hold != 1:
                hold = 1
                flag = i
                continue
            if prices[i + 1] < prices[i] and hold == 1:
                pric.append(prices[flag])
                pric.append(prices[i])
                hold = 0
            else:
                continue
        for i in range(0, len(pric), 2):
            temp.append(pric[i + 1] - pric[i])
            msum = sum(temp)
        if hold == 1:
            msum = msum + prices[-1] - prices[flag]
        return msum


l = Soulution()
print(l.maxProfit([7, 1, 5, 3, 6, 4]))
print(l.maxProfit([1, 2, 3, 4, 5]))
print(l.maxProfit([7, 6, 4, 3, 1]))

 给定一个整数数组prices,它的第i个元素prices[i]是一支给定的股票在第i天的价格。设计一个算法来计算你所能获得的最大利润,你最多可以完成K笔交易。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)

示例一:

输入:k=2, prices=[2, 4, 1]

输出:2

解释:在第一天(股票价格=2)的时候买入,在第二天(股票价格=4)的时候卖出,这笔交易所获得利润=4-2

示例二:

输入:k=2, prices=[3, 2, 6, 5, 0, 3]

输出:7

class Soulution:
    def maxProfit(self, k, prices):
        if not prices:
            return 0
        n = len(prices)
        max_k = n // 2
        if k >= max_k:
            res = 0
            for i in range(n - 1):
                res += max(0, prices[i + 1] - prices[i])
                return res
        else:
            max_k = k
        dp = [[[0] * 2 for _ in range(k + 1)] for _ in range(n)]
        for i in range(max_k + 1):
            dp[0][i][1] = -prices[0]
        for i in range(1, n):
            for k in range(1, max_k + 1):
                dp[i][k][0] = max(dp[i - 1][k][0], dp[i - 1][k][1] + prices[i])
                dp[i][k][1] = max(dp[i - 1][k][1], dp[i - 1][k - 1][0] - prices[i])
        return dp[n - 1][max_k][0]


l = Soulution()
print(l.maxProfit(k=2, prices=[2, 4, 1]))
print(l.maxProfit(k=2, prices=[3, 2, 6, 5, 0, 3]))
class Soulution(object):
    def maxProfit(self, prices):
        """

        :param prices: list[int]
        :return: int
        """
        temp = []
        if not prices or len(prices) ==1:
            return 0
        for i in range(len(prices)-1):
            temp.append(max(prices[i+1:])-prices[i])
        if max(temp) >=0:
            return max(temp)
        else:
            return 0

l = Soulution()
print(l.maxProfit([7, 1, 5, 3, 6, 4]))

 

你可能感兴趣的:(python,算法)