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:

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.

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

  public int maxProfit(int[] prices, int fee) {
if(null==prices||0==prices.length){
return 0;
}
int stock =-prices[0]-fee;
int noStock =0;
int preNoStock=0;
for(int i=1;i<prices.length;i++){
preNoStock=noStock;
noStock=Math.max(noStock,stock+prices[i]);//第i天无股票是第i-1天无股票和第i天卖股票的最大值
stock=Math.max(preNoStock-prices[i]-fee,stock);//第i天有股票是第i-1天有股票和第i天买股票的最大值
}
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. QTableView 二次整理

    一.设置可视化的组件 参考: http://www.cnblogs.com/ribavnu/p/4810412.html 二.常用基本属性 http://www.cnblogs.com/ribavnu ...

  2. Linux 目录结构_004

    前言 Linux文件系统层次标准,英文全称Filesystem Hierarchy Standard,英文简称FHS. 由于利用Linux来开发产品的团队和个人实在太多了,如果每个人都以自己的想法来配 ...

  3. PHP最全笔记(二)(值得收藏,不时翻看一下)

    /* [goto]5.3+ 版本 */用来跳转到程序中的某一指定位置该目标位置可以用目标名称 加上冒号来标记.PHP中的goto有一定限制,只能在同一个文件和作用域中跳转,    也就是说你无法跳出一 ...

  4. Provided id of the wrong type for class pojo.Books. Expected: class java.lang.Integer, got class java.lang.Long

    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please ...

  5. Java中浮点数的处理

    import java.text.DecimalFormat; String addGold = String.valueOf(new DecimalFormat("0").for ...

  6. [Hinton] Neural Networks for Machine Learning - Hopfield Nets and Boltzmann Machine

    Lecture 11 — Hopfield Nets Lecture 12 — Boltzmann machine learning Ref: 能量模型(EBM).限制波尔兹曼机(RBM) 高大上的模 ...

  7. [Python] 06 - Modules --> Packages

    故事背景 一.阶级关系 1. Programs are composed of modules.2. Modules contain statements.3. Statements contain ...

  8. linux-Centos 7下tftp-server服务的安装与配置

    TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间 进行简单文件传输的协议,提供不复杂.开销不大的文件传输服 ...

  9. 现在越来越喜欢用ajax传值了,这样能让网站的体验性很好,今天就总结了一下常用的

    这是不用循环的方法 就是传过来的是一位数组 //编辑党建分类 function gk_bj(id){ $.post("{:U('Luser/lei_edlt')}",{id:id} ...

  10. 转:关于ROWNUM的使用

    转载自:原文:https://blog.csdn.net/songsenkeji/article/details/4432942 ROWNUM的概念ROWNUM是一个虚假的列.它将被分配为 1,2,3 ...