weekly contest 55 Best Time to Buy and Sell Stock with Transaction Fee
来源:互联网 发布:摇头岭车神淘宝 编辑:程序博客网 时间:2024/06/02 02:35
题目
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 = 2Output: 8Explanation: The maximum profit can be achieved by:Buying at prices[0] = 1Selling at prices[3] = 8Buying at prices[4] = 4Selling at prices[5] = 9The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
分析
这题和之前的冷静的买卖袜子是一个题目
那我们先定义状态
buy[i] 表示第i天的买收益的最大
sell[i] 表示第i天的卖收益的最大
接着填写初时状态
buy[0] = - prices[0] ( 我们买了第一件)
sell[0] = 0 (没法卖,没有收益)
接着是状态转移
buy[i] = max( sell[i-1] - prices[i], buy[i-1]);
( 前一天卖了后今天买了, 前一天的最大)
sell[i] = max( buy[i-1] + prices[i] - fee , sell[i-1]);
代码
class Solution {public: int maxProfit(vector<int>& prices, int fee) { // buy[i] = max(sell[i-1]-price, buy[i-1]) // sell[i] = max(buy[i-1]+price, sell[i-1]) int n = prices.size() ; if( n <= 1 ) return 0 ; vector<int> buy ; vector<int> sell; buy.resize(n , 0); sell.resize( n , 0); buy[0] = - prices[0]; sell[0] = 0 ; for( int i= 1 ; i<prices.size() ; i++){ buy[i] = max( sell[i-1] - prices[i] , buy[i-1] ) ; sell[i] = max( buy[i-1] + prices[i] - fee , sell[i-1] ) ; } return sell[n-1]; }};
时间复杂度 O(N)
空间复杂度O(N)
阅读全文
0 0
- weekly contest 55 Best Time to Buy and Sell Stock with Transaction Fee
- Best Time to Buy and Sell Stock with Transaction Fee
- Best Time to Buy and Sell Stock with Transaction Fee
- Best Time to Buy and Sell Stock with Transaction Fee
- LWC 55:714. Best Time to Buy and Sell Stock with Transaction Fee
- 714. Best Time to Buy and Sell Stock with Transaction Fee
- Leetcode算法学习日志-714 Best Time to Buy and Sell Stock with Transaction Fee
- LeetCode714. Best Time to Buy and Sell Stock with Transaction Fee
- leetcode 714 Best Time to Buy and Sell Stock with Transaction Fee
- LeetCode #714 Best Time to Buy and Sell Stock with Transaction Fee
- LeetCode算法问题10 —— Best Time to Buy and Sell Stock with Transaction Fee
- 714. Best Time to Buy and Sell Stock with Transaction Fee
- 714. Best Time to Buy and Sell Stock with Transaction Fee
- Best Time to Buy and Sell Stock with Transaction Fee[LeetCode 714]
- Best Time to Buy and Sell Stock with Transaction Fee问题及解法
- [LeetCode] Algorithms-714. Best Time to Buy and Sell Stock with Transaction Fee
- [LeetCode] DP之 Best time to buy and sell stock with transaction fee
- leetcode练习 714. Best Time to Buy and Sell Stock with Transaction Fee
- 计算单词的个数
- Java 取经之路
- (转)解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/
- 一次性能优化,tps从400+到4k+
- 这样阐述你的设计作品,轻轻松松一稿过
- weekly contest 55 Best Time to Buy and Sell Stock with Transaction Fee
- TestBird烧烤趴,几维安全专属报名通道
- 层级数据模板 案例(HierarchicalDataTemplateWindow)
- 习题3.2
- 机器学习资源积累
- Solr主要配置文件之solrconfig.xml。
- 一个前端的自我修养
- 手把手教你完成App端-支付宝支付Android
- 这10个思路,能帮你设计出简单易用的网页