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 fee representing 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.

又是一道股票交易的题,之前已经有过类似的五道题了,fun4LeetCode大神的帖子做了amazing的归纳总结,有时间的话博主也写个总结。这道题跟Best Time to Buy and Sell Stock II其实最像,但是由于那道题没有交易费的限制,所以我们就无脑贪婪就可以了,见到利润就往上加。但是这道题有了交易费,所以当卖出的利润小于交易费的时候,我们就不应该卖了,不然亏了。所以这道题还是还是得用动态规划来做,按照fun4LeetCode大神的理论,本质其实是个三维dp数组,由于第三维只有两种情况,卖出和保留,而且第二维交易的次数在这道题中没有限制,所以我们用两个一维数组就可以了,sold[i]表示第i天卖掉股票此时的最大利润,hold[i]表示第i天保留手里的股票此时的最大利润。那么我们来分析递推公式,在第i天,如果我们要卖掉手中的股票,那么此时我们的总利润应该是前一天手里有股票的利润(不然没股票卖毛啊),加上此时的卖出价格,减去交易费得到的利润总值,跟前一天卖出的利润相比,取其中较大值,如果前一天卖出的利润较大,那么我们就前一天卖了,不留到今天了。然后来看如果第i天不卖的利润,就是昨天股票卖了的利润然后今天再买入股票,得减去今天的价格,得到的值和昨天股票保留时的利润相比,取其中的较大值,如果昨天保留股票的利润大,那么我们就继续保留到今天,所以递推时可以得到:

sold[i] = max(sold[i - 1], hold[i - 1] + prices[i] - fee);

hold[i] = max(hold[i - 1], sold[i - 1] - prices[i]);

参见代码如下:

解法一:

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

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

解法二:

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

类似题目:

Best Time to Buy and Sell Stock with Cooldown

Best Time to Buy and Sell Stock IV

Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock

参考资料:

https://discuss.leetcode.com/topic/107992/java-dp-solution-easy-understand

https://discuss.leetcode.com/topic/107977/c-concise-solution-o-n-time-o-1-space

https://discuss.leetcode.com/topic/107998/most-consistent-ways-of-dealing-with-the-series-of-stock-problems

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费的更多相关文章

  1. LeetCode Best Time to Buy and Sell Stock with Transaction Fee

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/descripti ...

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

  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] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. 714. Best Time to Buy and Sell Stock with Transaction Fee

    问题 给定一个数组,第i个元素表示第i天股票的价格,可执行多次"买一次卖一次",每次执行完(卖出后)需要小费,求最大利润 Input: prices = [1, 3, 2, 8, ...

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

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

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

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

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

随机推荐

  1. 2018.3.29 div格式设置

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  2. Oracle查询用户权限

    Oracle查询用户权限 -- 确定角色的权限select * from role_tab_privs ;              包含了授予角色的对象权限select * from role_ro ...

  3. fs输出文件目录

    var http = require("http"); var fs = require("fs"); var server = http.createServ ...

  4. python控制流 If-else

        控制流 If-else 我们处理现实生活中的问题时会做出决定,就像决定买哪种相机或者怎样更好的打篮球.同样我们写计算机程序的时候也要做相同的事情.我们通过 if-else 语句来做决定,我们使 ...

  5. Linux下Apache服务的查看和启动

      cd到/etc/rc.d/init.d/目录,并列出该目录下的所有文件,看看是否有httpd   使用httpd -v查看已经安装的httpd的版本   使用rpm -qa | grep http ...

  6. 第四篇:用IntelliJ IDEA 搭建基于jersey的RESTful api

    编译器:Intellij IDEA 系统环境: MAC OS 相关技术:Maven.tomcat 7.jdk8 1.创建项目 首先创建一个web Application项目(这里我们打算用maven引 ...

  7. js 时间戳 vue 时间戳的转换 ?

    在没有用vue做项目之前 也遇到过戳转换的问题 直接函数 调用 方法 这个也可以写成vue的  把function去掉  formatDate后面加冒号 就可以了 当然这个不是原创 但是是谁的我忘记了 ...

  8. 区间的连续段~ST表(模板题)

    链接:https://www.nowcoder.com/acm/contest/82/B来源:牛客网 时间限制:C/C++ 7秒,其他语言14秒 空间限制:C/C++ 262144K,其他语言5242 ...

  9. win-zabbix_agent端配置解析

    Zabbix agent 在windows上安装部署 部署windows服务器需要在agent服务器上添加到10.4.200.2的路由 蓝泰服务器需要添加10.0.90.5的网关,联通的机器需要添加到 ...

  10. JAVA中的Log4j

    Log4j的简介: 使用异常处理机制==>异常 使用debug调试(必须掌握)    System.out.Print(); 001.控制台行数有限制        002.影响性能      ...