题目如下:

Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer feerepresenting a transaction fee.

You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.)

Return the maximum profit you can make.

Example 1:

Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
Output: 8
Explanation: The maximum profit can be achieved by:
  • Buying at prices[0] = 1
  • Selling at prices[3] = 8
  • Buying at prices[4] = 4
  • Selling at prices[5] = 9
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

Note:

  • 0 < prices.length <= 50000.
  • 0 < prices[i] < 50000.
  • 0 <= fee < 50000.

解题思路:对于任意一天i来说,可以有跳过(cooldown包含在内)/买入/卖出三种操作,记dp[i][0],dp[i][1],dp[i][2]为第i天是做这三种操作时候可以获得的最大收益。很显然 dp[i][0] = max(dp[i-1][0],dp[i-1][1],dp[i-1][2]);dp[i][1]的情况分为第一次买入/非第一次买入,所以有 dp[i][1] = max(dp[i][1] ,-price[i],dp[j][2] - prices[i] ) (j<i) ,-prices[i]表示第一次买入,在当前节点获得的收益是负数,因为钱已经变成了股票,dp[j][2] - prices[i]表示本次买入是在第j天卖出后的后序操作,中间不存在其他的交易,这里还需要减去fee,因为每次卖出的时候需要收取手续费;同理dp[i][2] = max(dp[i][2],dp[j][1] + prices[i]-fee),这是因为卖出是要在买入之后。当然,对于每个i来说,并不需要去比较0~j天的所有数据,只要记录之前出现过的dp[j][1]和dp[j][2]的最大值即可。最后的结果是 max(0, dp[-1][0], dp[-1][2]),因为最后一天要么跳过,要么卖出,不会再有买入的操作。

代码如下:

class Solution(object):
def maxProfit(self, prices, fee):
"""
:type prices: List[int]
:type fee: int
:rtype: int
"""
if len(prices) <= 1:
return 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] - fee max_buy = max(dp[0][1], dp[1][1])
max_sell = -float('inf')
for i in range(1, 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]-fee)
dp[i][1] = max(dp[i][1], -prices[i], max_sell - prices[i])
max_sell = max(max_sell, dp[i][2])
max_buy = max(max_buy, dp[i][1])
#print dp
return max(0, dp[-1][0], dp[-1][2])

【leetcode】714. Best Time to Buy and Sell Stock with Transaction Fee的更多相关文章

  1. 【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  2. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  3. Week 7 - 714. Best Time to Buy and Sell Stock with Transaction Fee & 718. Maximum Length of Repeated Subarray

    714. Best Time to Buy and Sell Stock with Transaction Fee - Medium Your are given an array of intege ...

  4. 【leetcode】123. Best Time to Buy and Sell Stock III

    @requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...

  5. 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 714. Best Time to Buy and Sell Stock with Transaction Fee

    问题 给定一个数组,第i个元素表示第i天股票的价格,可执行多次"买一次卖一次",每次执行完(卖出后)需要小费,求最大利润 Input: prices = [1, 3, 2, 8, ...

  7. [LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee 买卖股票的最佳时间有交易费

    Your are given an array of integers prices, for which the i-th element is the price of a given stock ...

  8. 714. Best Time to Buy and Sell Stock with Transaction Fee有交易费的买卖股票

    [抄题]: Your are given an array of integers prices, for which the i-th element is the price of a given ...

  9. 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...

随机推荐

  1. 用maven给SpringBoot项目打包

    注意要点: 1.注意某个moule有依赖需要在对应的pom.xml里填写有关的信息,如: <dependencies> <dependency> <artifactId& ...

  2. PHP csv导出数据 (二)

    全部导出和时间导出 html代码,全程并不需要引用什么插件 <include file="public@header"/> <link href="__ ...

  3. 网络编程之TCP协议与UDP协议

    了解网络就要了解一些基本的协议今天主要跟大家分享一些关于TCP 协议UDP协议的相关知识 首先介绍一下TCP协议 TCP(Transmission Cintrol Protocol)可靠的.面向连接的 ...

  4. RESTful_URI资源

    目录 目录 RESTful的资源 URI 标识资源 URL 定位资源 URI与URL的区别 为什么使用资源的概念 对资源的操作 URI的设计 RESTful的资源 在RESTful基础知识篇中,介绍了 ...

  5. 《图解设计模式》读书笔记1-2 Adapter模式

    目录 Adapter即适配器,可以类比为将220V的电压的电源转为5V电压的手机充电器,起转换的作用. 明确概念: Adaptee:被适配者,即220v电压的电源 Adapter:适配器,即手机充电器 ...

  6. Nginx 转写功能和Apache .htaccess 对应

    之前一直使用apache 服务器,现有一项目想转到Nginx 服务器运行.. 发现Apache 的撰写功能和 Nginx的不一样.无法通用.hataccess 文件 查阅网上资料,nginx 配置转写 ...

  7. 精讲 org.w3c.dom(java dom)解析XML文档

    org.w3c.dom(java dom)解析XML文档 位于org.w3c.dom操作XML会比较简单,就是将XML看做是一颗树,DOM就是对这颗树的一个数据结构的描述,但对大型XML文件效果可能会 ...

  8. WEB服务端安全---认证会话与访问控制

    一.认证与会话管理 认证:简而言之就是通过一定的凭证认出用户是谁.认证过程中按凭证数量可简单分为 ‘单因素认证’.‘双因素认证’或多因素认证.一般来说,多因素认证强度更高,但是用户体验上会比单因素认证 ...

  9. python学习那点事---pycharm使用弹框问题如何解决

    学习python的目标:年后可以找一份不错的维护工作. 2019.11.4日是第一天开始学习python,从开始安装python3.6版本和pycharm开始.安装python版本非常顺利的就完成了, ...

  10. [Linux] 007 目录处理命令

    1. 目录处理命令:mkdir 命令名称:mkdir 命令英文原意:make directories 命令所在路径:/bin/mkdir 执行权限:所有用户 语法:mkdir -p [目录名] 功能描 ...