Leetcode——714. Best Time to Buy and Sell Stock with Transaction Fee
题目原址
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/
题目描述
给定一个数组和一个变量,其中数组中的元素表示当天的股票价格,变量表示每次交易需要支付的固定费用。不限制股票的购买和售出的次数,但是要求在购买新的股票之前要将手中的股票全部售出。
解题思路
- 分析题目,可以知道,主要就是考察股票的售出和购入问题。所以定义两个变量,ret和hold。ret用来表示第i天出售股票后的最大利润,hold表示保存第i天持有股票的最大利润。
- ret有两个状态:一个是保持第i天前一天的售出利润,另一个是售出第i天后的利润,ret选择其中的最大利润
- hold有两个状态:一个是在第i天购入的利润,另一个是第i天不购入的利润,选择其中的最大利润
- 这个题是一个dp问题,所以只要考虑当前天与前一天的关系即可。
AC代码
class Solution {public int maxProfit(int[] prices, int fee) {int ret = 0;int n = prices.length;if(n < 2)return ret;//hold保存当天持有股票的情况下的最大利润int hold = -prices[0];for(int i = 1; i < prices.length; i++) {//temp是当天售出后的最大利润int temp = ret;ret = Math.max(temp, hold + prices[i] - fee);hold = Math.max(hold, temp - prices[i]);}return ret; }
}
参考
https://blog.csdn.net/koala_tree/article/details/78530510
Leetcode——714. Best Time to Buy and Sell Stock with Transaction Fee相关推荐
- 【DP + 卖股票】LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee
LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee Solution1: 参考网址:http://www.cnblog ...
- leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp)
题目 https://leetcode.com/problems/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 ...
- LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题
此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...
- 【DP + 卖股票】LeetCode 309. Best Time to Buy and Sell Stock with Cooldown
LeetCode 309. Best Time to Buy and Sell Stock with Cooldown Solution1: 比较有难度的一道动态规划题了! 参考网址:http://z ...
- 【DP + 卖股票】LeetCode 188. Best Time to Buy and Sell Stock IV
LeetCode 188. Best Time to Buy and Sell Stock IV Solution1:我的答案 参考链接:http://www.cnblogs.com/grandyan ...
- 【DP + 卖股票】LeetCode 123. Best Time to Buy and Sell Stock III
LeetCode 123. Best Time to Buy and Sell Stock III Solution1: 不得不让人感叹算法之精妙啊!!! 参考网址:[1]http://www.cnb ...
- 【贪心 和 DP + 卖股票】LeetCode 122. Best Time to Buy and Sell Stock II
LeetCode 122. Best Time to Buy and Sell Stock II Solution1:我的答案 贪心和DP傻傻分不清! class Solution { public: ...
- 【贪心 和 DP + 卖股票】LeetCode 121. Best Time to Buy and Sell Stock
LeetCode 121. Best Time to Buy and Sell Stock Solution1:我的答案 动态规划和贪心不要区分的那么明显嘛~~~ class Solution { p ...
- [LeetCode]122. Best Time to Buy and Sell Stock II
[LeetCode]122. Best Time to Buy and Sell Stock II 题目描述 思路 I的后续 将数组分为几个小部分, 划分标准是 [i] < [i - 1](划分 ...
最新文章
- websphere 启动服务
- 计算机用于数据管理经历了,管理系统中计算机应用--期中测验答案
- python 多线程 popen ping指定IP是否在线 判断连通
- 【Java数据结构与算法】第四章 栈实现综合计算器
- 米斯特白帽培训讲义(v2)实战篇 余闻同学录
- easyui dialog 不执行页面js_Spring Security(六):前端菜单,角色权限页面的搭建
- Java21天打卡day18--继承
- 跨服务器、跨数据库、多表联合查询
- 解析大型.NET ERP系统 自动更新
- 微信小程序 java后勤服务管理系统app
- 数学分析与高等数学的异同
- 金融管理系统测试报告(内部测试案例)
- 手把手教你用开源软件anki有效学习、长期记忆
- python(11)- 文件处理
- young GC和Full GC的区别、什么时候触发young gc和Full GC、如何优化GC
- Percona监控工具初探
- 战斗在风口:社区团购从0到1实战运营笔记
- 咕泡P5人工智能深度学习高薪就业班
- 如何使用我们的“激活码”激活我们的在线EPF帐户? –第3部分
- div位置大小不随浏览器变化而变化