当前位置:网站首页>The best interests of buying and selling stocks with handling fees [What is missing in the definition of DP status?]
The best interests of buying and selling stocks with handling fees [What is missing in the definition of DP status?]
2022-08-02 07:50:00 【REN_Linsen】
找DPThe state defines what exactly is missing?
前言
Greedy examines logical analysis ability & Ability to abstract problems,The same goes for dynamic programming,How to transform the problem into a smaller scale but of the same nature,Really examines the ability to abstract problems,Not abstract,It feels like there are countless complex possibilities,Unable to start quiz!
一、Procedures for buying and selling stocks
二、贪心 & DP + 贪心
1、贪心
// 买股票的最佳时机.
public class MaxProfit {
/* be greedy:Hold the stock first,Because there is a fee for selling sharesfee,So the price at which the stock is held is the purchase price + fee. Sell when the price is higher than this,赚钱,But the latter may be more expensive,The optimal solution cannot be obtained locally,Then consider the selling price at this time as excessive price,The higher price in the back will directly take the difference as the profit. When touched than holding stock prices - fee low time,Take for sure not to buy that one in front,After all, you can't make money at this low point,Might as well hold this lower stock price. When hit lower than the holding stock price,But there's more than holding stock prices - fee高,则不管,After all, selling it is not only unprofitable,还要亏钱. 注:It is equivalent to converting the problem into a handling fee0of selling stocks,If and only if you buy a stock, count the handling fee first,就0手续费 + Don't do business at a loss. Greedy examines the ability of abstraction most:It doesn't really matter when you buy or sell,What matters is what is in the best interest. */
public int maxProfit(int[] prices, int fee) {
int buy = prices[0] + fee;
int profit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] + fee < buy) {
buy = prices[i] + fee;
} else if (prices[i] > buy) {
profit += prices[i] - buy;
// No formalities in the back.
buy = prices[i];
}
// Keep looking for opportunities to sell.
}
return profit;
}
}
2、DP + 贪心
// 一题多解,动态规划 + 贪心,Transform a large problem into subproblems with the same performance but smaller scale progressively.
class MaxProfit2 {
// Timeline in days,Earnings on the last day without holding the stock 为 max(前一天不持有股票,The stock held the day before was sold today)(贪心体现)
public int maxProfit(int[] prices, int fee) {
int n = prices.length;
int[][] f = new int[n][2];//1-状态定义
f[0][1] = -prices[0];//2-初始化
for (int i = 1; i < n; i++) {
//3-状态转移
f[i][0] = Math.max(f[i - 1][0], f[i - 1][1] + prices[i] - fee);
f[i][1] = Math.max(f[i - 1][1], f[i - 1][0] - prices[i]);
}
return f[n - 1][0];//4-最终状态
}
// 状压
public int maxProfit2(int[] prices, int fee) {
int n = prices.length;
int[] f = new int[2];
f[1] = -prices[0];
for (int i = 1; i < n; i++) {
int old = f[0];
f[0] = Math.max(old, f[1] + prices[i] - fee);
f[1] = Math.max(f[1], old - prices[i]);
}
return f[0];
}
/* review: 1-为什么我知道DPIt is applied to the decomposition of large problems to small-scale problems of the same nature,也知道DP四要素(状态定义/Status progresses/初始状态/最终状态),But can't think of the definition of state,In other words, the problem cannot be abstracted. A)DPDo less sense of innocence; B)Missing the key line--循环线,DPUsually one layer/两层/Three layers of loops to change state,Then the loop has to have a loop line,如按时间,If the question is in days,第i天. 一个len = n的数组,Claim to arrive at the last day's earnings,Is it possible to ask firstn-1的收益? */
}
总结
1)贪心/DPReally examine the problem abstraction ability,Not abstracting will feel the simulated complexity of the problem.
2)Abstraction is about grasping what the main problem is,Do not bring other unnecessary information.
3)知道DPIt is applied to the decomposition of large problems to small-scale problems of the same nature,也知道DP四要素(状态定义/Status progresses/初始状态/最终状态),But can't think of the definition of state,In other words, the problem cannot be abstracted?
A)DPDo less sense of innocence;
B)Missing the key line–循环线,DPUsually one layer/两层/Three layers of loops to change state,Then the loop has to have a loop line,如按时间,If the question is in days,第i天.
4)DPThe meaning of the state is what the question asks for,in the best interest of the subject;而DPThe position of each variable of the array,indicates when the best interest occurs.总结:什么时候 & 什么状态.
参考文献
边栏推荐
猜你喜欢
随机推荐
【机器学习】实验4布置:AAAI会议论文聚类分析
倍福使用AdsRemote组件实现和C#的ADS通讯
php删除一维数组中一个值
59:第五章:开发admin管理服务:12:MongoDB的使用场景;(非核心数据,数据量比较大的非核心数据,人脸照片等隐私的小文件;)
【心电信号】基于matlab心率检测【含Matlab源码 1993期】
概率论与数理统计
OC-error prompt
LeetCode Algorithm 1374. 生成每种字符都是奇数个的字符串
OC-NSSet(集合)
【机器学习】实验3布置:贝叶斯垃圾邮件识别
Splunk Filed extraction 字段截取
【故障诊断分析】基于matlab FFT轴承故障诊断【含Matlab源码 2001期】
Enterprise training and reproduction guidebook - training and reasoning of the OpenPose model based on Huawei ModelArts platform, realizing the recognition of two behaviors of climbing and climbing ov
OC-Category
Gradle系列——Gradle插件(基于Gradle文档7.5)day3-2
解决Pytorch模型在Gunicorn部署无法运行或者超时问题
OC - NSSet (set)
FormData上传二进制文件、对象、对象数组
Agile, DevOps and Embedded Systems Testing
21 days learning challenge 】 【 sequential search