问题

给定一个数组,第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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [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 ...

  4. 【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 ...

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

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

  6. 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 ...

  7. [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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. 剑指 offer set 23 n 个骰子的点数

    题目 把 n 个骰子扔到地上, 所有骰子朝上一面的点数之和为 s. 输入 n, 打印出 s 所有可能的值出现的概率. 思路 1. 基于递归的求解方法. 深度为 n 的 dfs, 相当于求全排列, 时间 ...

  2. Android屏幕和尺寸

    DisplayMetrics dm=new DisplayMetrics(); //获取的像素高度不包含虚拟键所占空间 ((WindowManager)context.getSystemService ...

  3. 安装PHP扩展-----phpredis

    一.redis介绍 redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了 memcached的不足,它支持存储的value类型相 ...

  4. Egret微端 创建项目(一)

    开发环境: window7 egret engine:5.0.14 egret wing:4.1.0 微端:v0.0.14 官方教程:http://developer.egret.com/cn/git ...

  5. XStream别名;元素转属性;去除集合属性(剥皮);忽略不需要元素

    city package xstream; public class City { private String name; private String description; public St ...

  6. JavaScript 禁止表单提交

    有时我们需要在表单真正提交之前,做一些检查工作,检查通过之后再进行提交. <form name="myForm" onsubmit = "validateMyFor ...

  7. 170413、怎么精确理解leader布置的任务(技术外的话)

    [缘起] 和一个同学交代了一个很重要的事情,结果执行的结果并不是自己想要的,微微生气之余,简单的聊聊“如何精确的理解leader布置的任务”. [员工角度的潜在困惑] 1)leader讲了很多,脑子记 ...

  8. Hadoop单点伪分布模式安装

    Hadoop单点伪分布模式安装 概述 单点 single-node,单节点,即一台计算机. 伪分布式模式 pseudo-distributed mode 所谓集群,表面上看是多台计算机联合完成任务:但 ...

  9. Spring---Bean使用外部属性文件

    在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean 配置相分离 Spring 提供了 ...

  10. Netty进行RPC服务器的开发 需要考虑的问题

    谈谈如何使用Netty开发实现高性能的RPC服务器 - Newland - 博客园 http://www.cnblogs.com/jietang/p/5615681.html 如何实现.基于什么原理? ...