题目如下:

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. [hadoop](3) MapReduce:创建计数器、任务状态和写入日志

    前言 本章主要讲述了如何在mapreduce任务中添加自定义的计数器,从所有任务中聚合信息,并且最终输出到mapreduce web ui中得到统计信息. 准备工作 数据集:ufo-60000条记录, ...

  2. CSS实现回到顶部图片hover后改变效果

    任何网站中回到顶部元素不可缺少,一般为了实现交互效果,都会在鼠标hover后元素样式有所改变.今天这个实例便是采用CSS中的transform知识实现. hover: <!DOCTYPE htm ...

  3. HDU 6121 Build a tree(k叉树的子树大小相异)

    http://acm.hdu.edu.cn/showproblem.php?pid=6121 题目大意: 给你一颗 n 个节点的完全 k 叉树,问你这棵树中所有子树结点个数的总异或值. 分析: 我们很 ...

  4. IHttpHandler

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ihttphandler?view=netframework-4.8 Defines th ...

  5. (转)深入理解Linux修改hostname

    当我觉得对Linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问题给我好好上了一课,很多知识点,当你觉得你已经掌握的时候,其实你了解的还只是皮毛.技术活,切勿浅尝则止! ...

  6. Linux-磁盘配额

    磁盘配额作用是限制普通用户使用的磁盘空间和创建文件的个数,不至于因为个别人的浪费而影响所有人的使用,需要内核的支持 注意:目前只有 ext2 ext3文件系统支持 需要用户程序quota程序包 先查看 ...

  7. 重写hashCode方法,导致内存泄漏

    package com.nchu.learn.base.reflect; import org.junit.Test; import java.util.Collection; import java ...

  8. jmeter3.0+ant1.10+jenkins实现接口自动化并发送邮件

    有很多关于接口自动化的文章,此篇仅用于记录自己的学习用.使用jmeter3.0+ant1.10+jenkins2.实现接口自动化并发送邮件,本篇是用的编写build文件来实现发送邮件,也可以用jenk ...

  9. 关于Vue+iview的前端简单的导入数据(excel)

    前一段时间项目经历了纯前端处理导入excel文件并处理等问题,数据量大的时候时间上长的一比,三千条数据需要三四秒甚至更长,不管产品咋想的,具体做法为: 首先下载一个这玩意: 进行简单封装一下: < ...

  10. 获取jQuery DataTables 的checked选中行

    $(function () { var  tabel = $('#userlist').DataTable({        destroy: true, //Cannot reinitialise ...