Leetcode 714. 买卖股票的最佳时机含手续费(DAY 27) ---- 动态规划学习期
发布日期:2021-06-30 22:24:39 浏览次数:3 分类:技术文章

本文共 1017 字,大约阅读时间需要 3 分钟。

文章目录


原题题目

在这里插入图片描述



代码实现(首刷超暴力解法)超时

int profit;void calculateprofit(int* prices,int pricesSize,int pos,int shares,int fee,int money){
if(money > profit) profit = money; if(pos < pricesSize) {
if(!shares) calculateprofit(prices,pricesSize,pos+1,prices[pos],fee,money-prices[pos]); else calculateprofit(prices,pricesSize,pos+1,0,fee,money+prices[pos]-fee); calculateprofit(prices,pricesSize,pos+1,shares,fee,money); }}int maxProfit(int* prices, int pricesSize, int fee){
profit = 0; calculateprofit(prices,pricesSize,0,0,fee,0); return profit;}


代码实现(半看解半自解) DP果然不好学

int maxProfit(int* prices, int pricesSize, int fee){
int dp[pricesSize][2],profit = 0,i; dp[0][0] = 0,dp[0][1] = (-prices[0]); for(i=1;i

代码实现(二刷自解 C++)

class Solution {
public: int maxProfit(vector
& prices, int fee) {
int size = prices.size(); int prebuy = -prices[0],presold = 0,nowbuy = 0,nowsold = 0; for(int i=1;i

转载地址:https://love6.blog.csdn.net/article/details/112862173 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Leetcode 96. 不同的二叉搜索树(DAY 28) ---- 动态规划学习期 (含题解)
下一篇:Leetcode 877. 石子游戏(DAY 27) ---- 动态规划学习期

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月22日 13时38分43秒