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 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的更多相关文章
- [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 ...
- 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 ...
- 714. Best Time to Buy and Sell Stock with Transaction Fee
问题 给定一个数组,第i个元素表示第i天股票的价格,可执行多次"买一次卖一次",每次执行完(卖出后)需要小费,求最大利润 Input: prices = [1, 3, 2, 8, ...
- 【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- @Transactional(readOnly=true) in Spring
http://www.skill-guru.com/blog/2010/12/19/transactionalreadonlytrue-in-spring/ @Transactional(readOn ...
- MTK 修改默认时区
首先介绍应用程序修改 : AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); mA ...
- IDEA 2017 安装后 关联SVN
IDEA 2017 安装后,SVN checkout时候会出现如下错误: Cannot run program "svn" (in directory "D:\demo\ ...
- Linux内核 设备树操作常用API
Linux设备树语法详解一文中介绍了设备树的语法,这里主要介绍内核中提供的操作设备树的API,这些API通常都在"include/of.h"中声明. device_node 内核中 ...
- linux zendOptimizer安装
ZendChina官方:下面介绍一下关于在linux环境下Zend Optimizer 3.3的安装方法.本篇文章是基于RHEL5架构的linux系统. (1)ZendOptimizer 3.3.3版 ...
- 更新docker时间-需要重启docker
更新docker时间:1.docker run -d -v /etc/localtime:/etc/localtime:ro [IMAGE] 2.重启,docker-compose up -d 3.d ...
- 堆的C语言实现
在C++中,可以通过std::priority_queue来使用堆. 堆的C语言实现: heap.c /** @file heap.c * @brief 堆,默认为小根堆,即堆顶为最小. */ #in ...
- UIScrollView _getDelegateZoomView bug 经历
[UIScrollView _getDelegateZoomView] UIKit -[UIScrollView_offsetForCenterOfPossibleZoomView:withIncom ...
- 树剖+线段树||树链剖分||BZOJ1984||Luogu4315||月下“毛景树”
题面:月下“毛景树” 题解:是道很裸的树剖,但处理的细节有点多(其实是自己线段树没学好).用一个Dfs把边权下移到点权,用E数组记录哪些边被用到了:前三个更新的操作都可以合并起来,可以发现a到b节点间 ...
- set @sql=N'q',这里的N是什么意思,加与不加有什么区别
用来支持UNICODE字符的,不加的话,如果sql语句中有Unicode字符会认不出来