Best Time to Buy and Sell Stock III Leetcode Python

Say you have an array for which the ith 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). 这道题目可以采用两种方法来解,第一种解法是 分别从左往右扫描一次 以及从右往左扫描一次依次把得到的最大的maxpro 放到数组里面 stack1 stack2 最后求解的时候 将stack1[i]+stack2[i+1] 得到的最大值就是最大值。代码如下

class Solution:
    # @param prices, a list of integer
    # @return an integer
    def maxProfit(self, prices):
        if len(prices)==0:
            return 0
        stack1=[]
        stack2=[]
        lowprice=prices[0]
        maxpro=0
        highprice=prices[-1]
        for index in range(len(prices)):
            lowprice=min(lowprice,prices[index])
            maxpro=max(maxpro,prices[index]-lowprice)
            stack1.append(maxpro)
        maxpro=0
        for index in reversed(range(len(prices))):
            highprice=max(highprice,prices[index])
            maxpro=max(maxpro,highprice-prices[index])
            stack2.insert(0,maxpro)

        for index in range(len(prices)-1):
            maxpro=max(maxpro,stack1[index]+stack2[index+1])
        return maxpro
            

第二种做法是用动态规划的方法解的

i表示第i天 j表示一共可以进行j次交易

全局最优的表达式为g[j]=max(g[j],l[j])

局部最优的表达为当前最优和 前一个全局最优加上当前可以挣的钱的最大 表达式为

l[j]=max(g[j-1]+max(dif,0),l[j]+dif)

具体代码如下:

class Solution:
    # @param prices, a list of integer
    # @return an integer
    def maxProfit(self, prices):
        g=[0,0,0]
        l=[0,0,0]
        for i in range(len(prices)-1):
            dif=prices[i+1]-prices[i]
            for j in reversed(range(1,3)):
                l[j]=max(g[j-1]+max(dif,0),l[j]+dif)
                g[j]=max(l[j],g[j])
        return g[2]
时间: 2024-10-28 21:36:58

Best Time to Buy and Sell Stock III Leetcode Python的相关文章

Best Time to Buy and Sell Stock III leetcode java

题目: Say you have an array for which the ith 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

leetcode 【 Best Time to Buy and Sell Stock III 】python 实现

题目: Say you have an array for which the ith 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 (

Best Time to Buy and Sell Stock III <leetcode>

Say you have an array for which the ith 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,

188. Best Time to Buy and Sell Stock IV Leetcode Python

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. Note: You may not engage in multiple transactions at the same time (ie, y

Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock III Say you have an array for which the ith 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. c++版本代码: class Solution { public: i

leetcode——Best Time to Buy and Sell Stock III 买卖股票最大收益(AC)

Say you have an array for which the ith 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,

【leetcode】Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock III Say you have an array for which the ith 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

LeetCode 笔记23 Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock III Say you have an array for which the ith 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. 现在A股涨这么好,要是股票都提前知道价格就好了(@[email pro

123. Best Time to Buy and Sell Stock III

/* * 123. Best Time to Buy and Sell Stock III * 2016-5-21 by Mingyang * 根据题目要求,最多进行两次买卖股票,而且手中不能有2只股票,就是不能连续两次买入操作. * 所以,两次交易必须是分布在2各区间内,也就是动作为:买入卖出,买入卖出. * 进而,我们可以划分为2个区间[0,i]和[i,len-1],i可以取0~len-1. * 那么两次买卖的最大利润为:在两个区间的最大利益和的最大利润. * 一次划分的最大利益为:Prof