给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。

你可以无限次地完成交易,但是你每次交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。

返回获得利润的最大值。

示例 1:

输入: prices = [1, 3, 2, 8, 4, 9], fee = 2
输出: 8
解释: 能够达到的最大利润:
在此处买入 prices[0] = 1
在此处卖出 prices[3] = 8
在此处买入 prices[4] = 4
在此处卖出 prices[5] = 9
总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
注意:

0 < prices.length <= 50000.
0 < prices[i] < 50000.
0 <= fee < 50000.

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee

方法:动态规划

我们维护两个变量 cash 和 hold,前者表示当我们不持有股票时的最大利润,后者表示当我们持有股票时的最大利润。

在第 i 天时,我们需要根据第 i−1 天的状态来更新 cash 和 hold 的值。对于 cash,我们可以保持不变,或者将手上的股票卖出,状态转移方程为

cash = max(cash, hold + prices[i] - fee)

对于 hold,我们可以保持不变,或者买入这一天的股票,状态转移方程为

hold = max(hold, cash - prices[i])

在计算这两个状态转移方程时,我们可以不使用临时变量来存储第 i−1 天 cash 和 hold 的值,而是可以先计算 cash 再计算 hold,原因是在同一天卖出再买入(亏了一笔手续费)一定不会比不进行任何操作好。

python

class Solution(object):
def maxProfit(self, prices, fee):
cash, hold = 0, -prices[0]
for i in range(1, len(prices)):
cash = max(cash, hold + prices[i] - fee)
hold = max(hold, cash - prices[i])
return cash

java

class Solution {
public int maxProfit(int[] prices, int fee) {
int cash = 0, hold = -prices[0];
for (int i = 1; i < prices.length; i++) {
cash = Math.max(cash, hold + prices[i] - fee);
hold = Math.max(hold, cash - prices[i]);
}
return cash;
}
}

复杂度分析

时间复杂度:O(n),其中 n 是 prices 数组的长度。

空间复杂度:O(1)。

解法一:

class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
vector<int> sold(prices.size(), 0), hold = sold;
hold[0] = -prices[0];
for (int i = 1; i < prices.size(); ++i) {
sold[i] = max(sold[i - 1], hold[i - 1] + prices[i] - fee);
hold[i] = max(hold[i - 1], sold[i - 1] - prices[i]);
}
return sold.back();
}
};

我们发现不管是卖出还是保留,第i天到利润只跟第i-1天有关系,所以我们可以优化空间,用两个变量来表示当前的卖出和保留的利润,更新方法和上面的基本相同,就是开始要保存sold的值,不然sold先更新后,再更新hold时就没能使用更新前的值了,参见代码如下:

解法二:

class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
int sold = 0, hold = -prices[0];
for (int price : prices) {
int t = sold;
sold = max(sold, hold + price - fee);
hold = max(hold, t - price);
}
return sold;
}
};

LeetCode——714. 买卖股票的最佳时机含手续费.的更多相关文章

  1. Java实现 LeetCode 714 买卖股票的最佳时机含手续费(动态规划 || 迭代法)

    714. 买卖股票的最佳时机含手续费 给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 :非负整数 fee 代表了交易股票的手续费用. 你可以无限次地完成交易,但是你每次交 ...

  2. leetcode 714. 买卖股票的最佳时机含手续费

    继承leetcode123以及leetcode309的思路,,但应该也可以写成leetcode 152. 乘积最大子序列的形式 class Solution { public: int maxProf ...

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

  4. 【LeetCode】714、买卖股票的最佳时机含手续费

    Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...

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

  6. 每日一题-——LeetCode(121)买卖股票的最佳时机

    题目描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格.如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润.注意你不能在买入股票前卖出股票 ...

  7. Leetcode 188.买卖股票的最佳时机IV

    买卖股票的最佳时机IV 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 k 笔交易. 注意: 你不能同时参与多笔交易(你必 ...

  8. LeetCode《买卖股票的最佳时机》系列题目,最详解

    目录 说在前面 引例:只能交易一次 一.动态数组定义 二.状态转移方程 三.初始化 四.优化 无限制买卖 一.动态数组定义 二.状态转移方程 三.初始化 四.优化 交易 2 次,最大利润? 一.动态数 ...

  9. Leetcode——121. 买卖股票的最佳时机

    题目描述:买卖股票的最佳时机 题目要求求解能获得最大利润的方式? 可以定一个二维数组 d [ len ] [ 2 ] ,其中d[ i ][ 0 ] 表示前i天可以获得的最大利润:d[ i ][ 1 ] ...

随机推荐

  1. Python的一些常用知识

    1.How to force urllib2 not to use a proxy Here is an example to remove proxy settings for all reques ...

  2. Python序列内单双引的问题——未解决

    在学习python基础的时候,遇到这样一个问题: tuple=(2,2.3,"yeah",5.6,False)list=[True,5,"smile"] 这样输 ...

  3. Swift 结构体struct

    //结构体是一个值类型 struct location{ //属性 var x:Double var y:Double //方法 func test() { print("结构体中的test ...

  4. [转]Spark SQL2.X 在100TB上的Adaptive execution(自适应执行)实践

    Spark SQL是Apache Spark最广泛使用的一个组件,它提供了非常友好的接口来分布式处理结构化数据,在很多应用领域都有成功的生产实践,但是在超大规模集群和数据集上,Spark SQL仍然遇 ...

  5. 导出execl

    string filepath = Utils.GetMapPath("/upload/excel/"); filepath = filepath + fileName + &qu ...

  6. Idea 打印GC

    设置 Run ⇒ Edit Configurations ⇒ VM options 添加 -XX:+PrintGCDetails 运行程序后会在末尾打印GC信息 2019-11-02 13:07:47 ...

  7. distpicker.js 根据当前位置初始化select

    学习参考的地址放在最醒目的地方: https://blog.csdn.net/idea_boy/article/details/58280076 百度官方实例:http://developer.bai ...

  8. Centos 7 x86_64 环境Python2.7升级Python3.7.4

    升级Python3.7.4 #安装补丁包yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel read ...

  9. 使用BP爆破有token值的密码(10.17 第二十三天)

    此次爆破使用的网站是DVWA来进行测试 工具:Burp suite 1.进去DVWA网站,选择高级安全等级,再进入到Brute Force(爆破)模块,假设此时已知账号是admin的情况下我们不知道密 ...

  10. UVA - 10791 Minimum Sum LCM(最小公倍数的最小和)

    题意:输入整数n(1<=n<231),求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小.输出最小的和. 分析: 1.将n分解为a1p1*a2p2……,每个aipi作为一个单独 ...