博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】309. Best Time to Buy and Sell Stock with Cooldown
阅读量:7060 次
发布时间:2019-06-28

本文共 2272 字,大约阅读时间需要 7 分钟。

题目如下:

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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

  • You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
  • After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)

Example:

Input: [1,2,3,0,2]Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]

题目如下:本题和  几乎是一样的,一个是有交易手续费,一个是有CD时间。在 的基础上,dp[i][0] (跳过) 和dp[i][2]出售都是一样的,不同之处在于dp[i][1] ,这里有至少一天的CD,所以修改状态状态转移方程为:dp[i][1] = max(dp[i][1], -prices[i], dp[j][2] - prices[i]) (这里i > 2,j < i-1 ),而在i = 2的时候,就只有 dp[i][1] = max(dp[i][1], -prices[i])。

代码如下:

class Solution(object):    def maxProfit(self, prices):        """        :type prices: List[int]        :rtype: int        """        if len(prices) <= 1:            return 0        elif len(prices) == 2:            return max(0, prices[1] - prices[0])        dp = []        for i in prices:            dp.append([-float('inf'),-float('inf'),-float('inf')])  #0:do nothing, 1:buy ,2:sell        dp[0][1] = -prices[0]        dp[1][1] = -prices[1]        dp[1][2] = prices[1] - prices[0]        max_buy = max(dp[0][1],dp[1][1])        max_sell = dp[1][2]        for i in range(2,len(dp)):            dp[i][0] = max(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2])            dp[i][2] = max(dp[i][2], max_buy + prices[i])            if i == 2: # the 3rd day can but stocks only there are no transactions in the first two days                dp[i][1] = max(dp[i][1], -prices[i])            else:                dp[i][1] = max(dp[i][1], -prices[i], max_sell - prices[i])            max_sell = max(max_sell, dp[i-1][2]) #cooldown day,so update max_sell in i-1 day            max_buy = max(max_buy, dp[i][1])            '''            for j in range(i):                if j != i-1: #cooldown day                    dp[i][1] = max(dp[i][1],-prices[i],dp[j][2] - prices[i])                dp[i][2] = max(dp[i][2],dp[j][1] + prices[i])            '''        #print dp        return max(0,dp[-1][0],dp[-1][2])

 

转载于:https://www.cnblogs.com/seyjs/p/10578031.html

你可能感兴趣的文章
docker 部署mvc项目 <四>
查看>>
HTML5漫谈(7)——如何保护HTML5应用代码
查看>>
C语言 gets()和scanf()函数的区别
查看>>
Android 虚拟键盘弹出把底部栏顶上去的解决办法
查看>>
POJ-2112 Optimal Milking(最大流)未完待续~
查看>>
解决问题:Appium Android WebView 测试执行,从源生切换至WebView不稳定。超时后报chrome not reachable...
查看>>
day03_函数
查看>>
Django启程篇
查看>>
Educational Codeforces Round 24 E. Card Game Again[数论][线段树]
查看>>
谈谈游戏
查看>>
Javascript ----函数表达和形参实参
查看>>
PHP 防恶意刷新实现代码
查看>>
全文检索、数据挖掘、推荐引擎系列3---全文内容推荐引擎之中文分词
查看>>
软件工程—软件可靠性测试
查看>>
个人阅读计划
查看>>
SQL Server 查看数据页面
查看>>
angularJS的过滤器!
查看>>
微信小程序 --- Image组件
查看>>
sql 获取一个周的周一和周日
查看>>
zepto源码分析-代码结构【转载】
查看>>