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. RMAN正确地删除Archivelog以及设置有备库的归档删除策略

    原文链接:http://blog.sina.com.cn/s/blog_64e166580100xks5.html 如何正确地删除Archivelog: Archivelog并不能直接得从OS层直接物 ...

  2. USI和USCI的区别

    在 MSP430 系列中微控制器中有三种串行通讯模块.它们分别是 USART . USI 和 USCI . USART 支持同一硬件模块的两种串行模式,分别是 UART 和 SPI . USART 实 ...

  3. 说说自己对RESTful API的理解

    REST不是英文上的rest单词,其英文缩写为presentational State Transfer ,直译为表现状态转移,咋看起来很学术,不懂,其实不用去死抠这个词的意思.REST是一种约束和架 ...

  4. 关于Solaris 的磁盘的分区

    也许是深受LINUX  Windows  fdisk 影响,每次看完Solaris的format命令,总是云里雾里.我今天总结一下,各位给点指点 一. Linux.Windows  传统的磁盘区层级, ...

  5. asp.net Identity 设置自定义登录

    添加Startup.Auth.cs public partial class Startup { // For more information on configuring authenticati ...

  6. VMware Workstation11安装Mac OS X 10.10虚拟机

    原文:http://jingyan.baidu.com/article/3f16e003eac66e2591c103e0.html 优化:http://www.cnblogs.com/yipu/p/4 ...

  7. 【python】用python生成pdf文件

    转自:https://www.davidfischer.name/2015/08/generating-pdfs-with-and-without-python/ from reportlab.pla ...

  8. Maven传递依赖的坑:父pom中dependencyManagement版本优先级高于传递依赖版本

    一.由来 之前同事问了个问题,就是当前工程为spring boot项目,假设版本号为2.0.3 这个项目中依赖了一个spring boot项目依赖(先别管为啥有这么奇葩的依赖,这个版本是1.5.9). ...

  9. A - Wall

    Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the ...

  10. [Android实例] Android Studio插件-自动根据布局生成Activity等代码1.4 (开源)(申明:来源于网络)

    [Android实例] Android Studio插件-自动根据布局生成Activity等代码1.4 (开源)(申明:来源于网络) 地址:http://www.eoeandroid.com/thre ...