714. Best Time to Buy and Sell Stock with Transaction Fee
问题
给定一个数组,第i个元素表示第i天股票的价格,可执行多次“买一次卖一次”,每次执行完(卖出后)需要小费,求最大利润
Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
Output: 8
Explanation: ((8 - 1) - 2) + ((9 - 4) - 2) = 8
思路和代码
在某天交易(或选择不操作)之后,有两个状态,要么手有股票,要么手中没有股票,我们用两个状态数组来表示。hava_stock表示有股票,no_stock表示没有股票。
have_stock[i]表示第i天结束后(此时手中有股票)最大利润。
no_stock[i]表示第i天结束后(此时手中没股票)的最大利润。
如果当天操作结束后,你手头没有股票的话,那么你:要么是今天卖了股票(昨天是有股票的),要么是保持了昨天的状态,只需要在这两者取最大即可。no_stock[i] = max(have_stock[i-1]+prices[i]-fee, no_stock[i-1])。
如果当天操作结束后,你手头有股票的话,那么你:要么是今天买了股票(昨天是没有股票的),要么是保持了昨天的状态,只需要在这两者取最大即可。have_stock[i] = max(no_stock[i-1]-prices[i], have_stock[i-1])。
返回最后一天的no_stock即可,因为完成交易获得最大利润时,手头肯定是没有股票的。
时间复杂度O(n),空间复杂度O(n)
class Solution(object):
def maxProfit(self, prices, fee):
"""
:type prices: List[int]
:type fee: int
:rtype: int
"""
no_stock = [0]*len(prices)
have_stock = [0]*len(prices)
have_stock = -prices[0]
for i in range(1,len(prices)):
no_stock[i] = max(have_stock[i-1]+prices[i]-fee, no_stock[i-1])
have_stock[i] = max(no_stock[i-1]-prices[i], have_stock[i-1])
return no_stock[len(prices)-1]
优化
由于两个dp数组中状态都取决于前一天,可以进行优化,省去dp数组开销。
对于no_stock的max计算,直接去掉数组索引,计算前的变量have_stock[i-1]和no_stock[i-1]表示前一天的,直接写成have_stock和no_stock即可,计算后的变量no_stock[i]表示今天的,写成no_stock即可。
对于have_stock的max计算,have_stock[i-1]也可以直接写成have_stock表示前一天的,而no_stock[i-1]不能写成no_stock,因为在上一步计算(no_stock的计算中可能覆盖了),所以可以用一个tmp在no_stock计算之前暂存起来。
tmp = no_stock
no_stock = max(have_stock+prices[i]-fee, no_stock)
have_stock = max(tmp-prices[i], have_stock)
事实上这个临时变量也可以省去。考虑no_stock的max操作,当no_stock较大时当然不需要用tmp来暂存前一天的no_stock,因为前一天跟今天的一样。而have_stock+prices[i]-fee较大时可以得到have_stock > no_stock - prices[i],此时have_stock的max计算会直接取到have_stock,不会用到no_stock,所以不用担心no_stock被改变后影响have_stock的max计算。
时间复杂度O(n),空间复杂度O(1)
class Solution(object):
def maxProfit(self, prices, fee):
"""
:type prices: List[int]
:type fee: int
:rtype: int
"""
no_stock = 0
have_stock = -prices[0]
for i in range(1,len(prices)):
no_stock = max(have_stock+prices[i]-fee, no_stock)
have_stock = max(have_stock, no_stock-prices[i])
return no_stock
类似题目
121. Best Time to Buy and Sell Stock
714. Best Time to Buy and Sell Stock with Transaction Fee的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- 【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 ...
- 【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee)
Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee) 股票问题: 1 ...
- [LeetCode] 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 ...
- [Swift]LeetCode714. 买卖股票的最佳时机含手续费 | 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 ...
- 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 ...
随机推荐
- sql server函数(isnull,charindex,cast,自定义函数)
SELECT charindex( CAST ( dbo.ufn_IsNullOrEmpty ('109722A3-622D-4FD4-A060-0287C933A89E', a.OUID) AS V ...
- 复习及总结--.Net线程篇(4)
这里要说的就是多线程的锁的问题了 锁:作用在于实现线程间的同步问题,最典型的是售票问题 1,InterLocked 提供的都是静态方法,用来同步对多个共享变量的访问,包括以原子方式递增,递减,比较和替 ...
- iOS开发之--NSNotificationCenter的使用
NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广 iOS中通知中心NSNotificationCenter应用总结 一.了解几个相关的 ...
- HashMap实现原理、核心概念、关键问题的总结
简单罗列一下较为重要的点: 同步的问题 碰撞处理问题 rehash的过程 put和get的处理过程 HashMap基础: HashMap的理论基础:维基百科哈希表 JDK中HashMap的描述:Has ...
- 使用async/await——Nodejs+ExpressJs+Babel
在使用诸如restify/expressjs等Nodejs Web Framework时,我们最头疼的问题就是回调黑洞. 虽然后又Koa/Koa2号称“The Next Framework”使用co解 ...
- JAVA基础之multipart,urlencoded以及JSON
一.(enctype) 表单的默认编码方式 ajpplication/x-www-form-urlencoded 上传文件的编码方式 multipart/form-data 互联网应用常用编码 ...
- SpringBoot--属性加载顺序
属性加载顺序: 1.在命令行中传入的参数: 2.SPRING_APPLICATION_JSON中的属性:SPRING_APPLICATION_JSON是以JSON格式配置在系统环境变量中内容: 3.j ...
- c# 日常记录,(获取系统时间、return),一些文件隐藏无法引用,c#多个窗体之间传值
1.获取系统时间 DateTime.Now.ToString(); DateTime dt =DateTime.Now; dt.AddDays(1); //增加一天 dt.AddDays(-1);// ...
- MySQL数据库主从同步延迟分析及解决方案
一.MySQL的数据库主从复制原理 MySQL主从复制实际上基于二进制日志,原理可以用一张图来表示: 分为四步走: 1. 主库对所有DDL和DML产生的日志写进binlog: 2. 主库生成一个 lo ...
- dstat命令--检查linux系统性能
系统性能的相关工具:vmstat.netstat.iostat.ifstat. dstat命令可以查看虚拟内存.网络连接和接口.cpu活动.输入/输出设备等.