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 ...
随机推荐
- java基础---->数组的基础使用(一)
数组是一种效率最高的存储和随机访问对象引用序列的方式,我们今天来对数组做简单的介绍.手写瑶笺被雨淋,模糊点画费探寻,纵然灭却书中字,难灭情人一片心. 数组的简单使用 一.数组的赋值 String[] ...
- 【BZOJ2905】背单词 fail树+DFS序+线段树
[BZOJ2905]背单词 Description 给定一张包含N个单词的表,每个单词有个价值W.要求从中选出一个子序列使得其中的每个单词是后一个单词的子串,最大化子序列中W的和. Input 第一行 ...
- ios UITableView高度自适应(转)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // ...
- <link>标签的rel属性全解析
<link>标签定义了当前文档与 Web 集合中其他文档的关系.link 元素是一个空元素,它仅包含属性.此元素只能存在于 head 部分,不过它可出现任何次数.在 HTML 中,&l ...
- java URL、HTTP与HTML+CSS
一.Web三大基石 1 二.API(Application Programming Interface,应用程序编程接口) 1 三.题目分析总结: 3 五.HTTP协议与寄信是类似的 6 请求报文 6 ...
- 基于注解的形式配置Bean
基于注解的方式配置Bean:也就说我们在每个Bean的类名前面注解一下,Spring会自动帮我们扫描Bean放进IOC容器中 I基于注解的方式配置Bean(没有依赖关系的Bean)有两个步骤: 1组件 ...
- CodeForces 24D Broken robot (概率DP)
D. Broken robot time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- swift 下storyboard的页面跳转和传值
------------------1. 最简单的方法 拖拽, 这个就不用多解释了吧. 直接拖拽到另一个视图控制器, 选择 show, 就行了. 2. 利用 Segue 方法 (这里主要是 方法1 的 ...
- c# 下三角实现 九九乘法口诀表
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Hell ...
- 使用MFC做D3D的框架
转载请注明出处http://www.cnblogs.com/CAION/p/3192111.html (程序运行时是和其他程序挺像 = =,但我保证这是原创的) 1.将D3D的初始化,渲染等等一些行为 ...