题目如下:

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. [CSP-S模拟测试]:旋转子段(数学)

    题目描述 $ZYL$有$N$张牌编号分别为$1,2,...,N$.他把这$N$张牌打乱排成一排,然后他要做一次旋转使得旋转后固定点尽可能多.如果第$i$个位置的牌的编号为$i$,我们就称之为固定点.旋 ...

  2. HDU6672 Seq(找规律)

    HDU6672 Seq 对于递推式 \(a_n = (\sum_{i = 1}^{n-1}a_i * i)\%n\) 打表列出 \(a_i\) 的前 \(100\) 项,发现有以 \(6\) 为循环的 ...

  3. 深入学习Keras中Sequential模型及方法

    https://www.cnblogs.com/wj-1314/p/9579490.html

  4. day08—css布局解决方案之多列布局

    转行学开发,代码100天——2018-03-24 本文将记录CSS布局之垂直布局解决方案. 常见的多列布局包括以下: 1.定宽+自适应 2.两列定宽+一列自适应 3.不定宽+自适应 4.两列不定宽+一 ...

  5. The server time zone value 'EDT' is unrecognized or represents more than one time zone

    解决: (1)使用 server mysql start命令启动mysql (2)在mysql中执行show variables like '%time_zone%'; (3)输入select now ...

  6. jmeter之2种方法录制脚本

    有时候候录制脚本比写脚本且快,这时候我们可以采用录制的方法完成jmeter脚本工作 目录 1.badboy录制 2.代理服务器录制 1.badboy录制 第一步:下载安装 badboy2.2下载:链接 ...

  7. 表单组件中state依赖props

    参阅避免派生状态的博文: https://zh-hans.reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html

  8. Unity shader with lightmap

    小记一下用法与问题,时更 surface shader就不用操心了,自带lightmap计算 主要是vertex fragment shader部分 Unity5 bake light map有三种情 ...

  9. springboot启动脚本

    #!/bin/sh JAVA_HOME="/ulic1/jdk/jdk1.8.0_201/bin" export JAVA_HOME lsof -i:9010 |awk '{pri ...

  10. Collections -集合排序compareTo方法重写,shuffle,addall

    package cn.learn.collection.Collections; /* 排序的对象的类,实现comparable借口,重写compareto方法 若要打印必须重写toString方法, ...