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 feerepresenting 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:

  1. Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
  2. Output: 8
  3. 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
  1. The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

Note:

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

使用dp 时间复杂度为O(n) ,只记录上一个点的信息,即空间复杂度为O(1)

  1. public int maxProfit(int[] prices, int fee) {
  2. if(null==prices||0==prices.length){
  3. return 0;
  4. }
  5. int stock =-prices[0]-fee;
  6. int noStock =0;
  7. int preNoStock=0;
  8. for(int i=1;i<prices.length;i++){
  9. preNoStock=noStock;
  10. noStock=Math.max(noStock,stock+prices[i]);//第i天无股票是第i-1天无股票和第i天卖股票的最大值
  11. stock=Math.max(preNoStock-prices[i]-fee,stock);//第i天有股票是第i-1天有股票和第i天买股票的最大值
  12. }
  13. return noStock;

相关题

买卖股票的最佳时间1 LeetCode121 https://www.cnblogs.com/zhacai/p/10429264.html

买卖股票的最佳时间2 LeetCode122 https://www.cnblogs.com/zhacai/p/10596627.html

买卖股票的最佳时间3 LeetCode123 https://www.cnblogs.com/zhacai/p/10645571.html

买卖股票的最佳时间4 LeetCode188 https://www.cnblogs.com/zhacai/p/10645522.html

买卖股票的最佳时间冷冻期 LeetCode309 https://www.cnblogs.com/zhacai/p/10655970.html

LeetCode-714.Best Time to Buy and Sell Stock with Transaction Fee的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  10. [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. java调用删除文件的方法删除文件,却删除不干净

    场景: 程序中在做数据下载时,生成了一个临时文件夹.夹子里面有一些txt和其他格式文件. 数据下载完毕后,需要删除这个临时文件夹,但是一直删除不干净,总会有一下文件残留. 网搜到了这个问题的原因: 内 ...

  2. Ubuntu14.04下安装redis-3.2.0以及开机自启动

    去官网下载Redis-3.2.0.tar.gz,将redis-3.2.0.tar.gz放入/opt目录下 解压redis-3.2.0.tar.gz xiaoyao@xiaoyao-virtual-ma ...

  3. SQLServer最耗资源时间的SQL语句

    作者kolachen http://blog.sina.com.cn/s/blog_63f3e0060102vcm0.html 先拷过来了,等有空再研究一下 执行最慢的SQL语句 SELECT (to ...

  4. django通用视图(类方法)

    这周是我入职的第一周,入职第一天看到嘉兴大佬的项目代码.视图中有类方法,我感到很困惑. 联想到之前北京融360的电话面试,问我有无写过类方法……看来有必要了解下视图的类方法,上网搜了很多,原来这就是所 ...

  5. jquery.validate 验证记录

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  6. [转]sudo找不到命令:修改sudo的PATH路径

    sudo有时候会出现找不到命令,而明明PATH路径下包含该命令,让人疑惑.其实出现这种情况的原因,主要是因为当 sudo以管理权限执行命令的时候,linux将PATH环境变量进行了重置,当然这主要是因 ...

  7. MSI/MSI-X Capability结构 (转)

    http://blog.sina.com.cn/s/blog_6472c4cc0102dskj.html

  8. 2018ACM-ICPC焦作区域赛【反思总结】

    摸银结束回来,整个人都轻松了. 自CCPC打铁以来的这两个月真的太痛苦了. 俱乐部退役的退役停训的停训,好冷清啊. 前期切题很稳,前四题两个小时1A. 过了四题之后好像心态有点飘,然后开题就慢了,想题 ...

  9. c语言笔记 数组2

    15. c99以前一直使用 gets 和 puts来输入输出字符串,但是gets因为无法获知内存大小,容易出现内存溢出(对此c99对gets,采取保留态势,c11直接废除,但是某些编译器仍然默认可以使 ...

  10. [No0000129]WPF(1/7)开始教程[译]

    概要 在我使用了半年多 WPF 后,是时候写点关于 WPF 基础方面的东西了.我发表了一系列针对具体问题的文章.现在是到了让大家明白为什么说WPF是一款在界面开发上带来革命的产品了. 本文针对初级-中 ...