当前位置: 首页 > news >正文

10.制作一个网站一般先要明确( )站内关键词排名软件

10.制作一个网站一般先要明确( ),站内关键词排名软件,建筑公司企业信用分,手机网站营销页309. 买卖股票的最佳时机含冷冻期 - 力扣(LeetCode) 动态规划解题思路: 1、暴力递归(难点如何定义递归函数) 2、记忆化搜索-傻缓存法(根据暴力递归可变参数确定缓存数组维度) 3、严格表结构依…

309. 买卖股票的最佳时机含冷冻期 - 力扣(LeetCode)

        动态规划解题思路:

1、暴力递归(难点如何定义递归函数)

2、记忆化搜索-傻缓存法(根据暴力递归可变参数确定缓存数组维度)

3、严格表结构依赖的动态规划

4、进一步优化(斜率优化、空间优化),非必须

一、分析:假设[0,index-1]之前的最大利润已经知道,现在计算到了index位置的最大利润。根据题意,到index位置后可能有三种状态,买入、卖出、冷冻。所以递归函数如下。

    public int maxProfit(int[] prices) {return p(prices,0,0);}// 0-买入 1-卖出 2-冷冻// 递归函数表示 从index位置到prices.length-1位置的最大利润public int p(int[] prices, int index, int state) {if (index == prices.length) {return 0;}int res = 0;if (state == 0) {// 买入 当前indexint p1 = -prices[index] + p(prices, index + 1, 1);// 不买当前indexint p2 = p(prices, index + 1, 0);return Math.max(p1, p2);} else if (state == 1) {// 卖当前indexint p1 = prices[index] + p(prices, index + 1, 2);// 不卖当前indexint p2 = p(prices, index + 1, 1);return Math.max(p1, p2);} else {//冷冻return p(prices, index + 1, 0);}}

二、我们直接看表依赖,略过傻缓存法。根据暴力递归函数分析,有两个可变参数,所以申请一个二维数组即可,看递归的base case初始化dp数组;看依赖关系index位置只依赖于index+1位置的元素,所以填数组的顺序为index倒序。返回值看递归调用的参数值是什么,就返回dp数组哪个位置的值。

    public int maxProfit2(int[] prices) {int N = prices.length;int[][] dp = new int[N + 1][3];for (int index = N - 1; index >= 0; index--) {dp[index][2] = dp[index + 1][0];dp[index][1] = Math.max(prices[index] + dp[index + 1][2], dp[index + 1][1]);dp[index][0] = Math.max(-prices[index] + dp[index][1], dp[index + 1][0]);}return dp[0][0];}

三、我们分析知道index位置只依赖于index+1位置的元素,也就是说index行的数据只依赖index+1行的数据,所以可以只用一个一维数组,循环更新即可。

    public int maxProfit(int[] prices) {int N = prices.length;//空间优化int[] dp = new int[3];for (int index = N - 1; index >= 0; index--) {int temp = dp[0];dp[0] = Math.max(-prices[index] + dp[1], dp[0]);dp[1] = Math.max(prices[index] + dp[2], dp[1]);dp[2] = temp;}return dp[0];}

 714. 买卖股票的最佳时机含手续费 - 力扣(LeetCode)

        该题的解法和上题类似,只是多了手续费,少了冷冻期。

一、暴力递归

    int f=0;public int maxProfit1(int[] prices, int fee) {f = fee;return p(prices, 0, 0);}// 0-买入 1-卖出public int p(int[] prices, int index, int state) {if (index == prices.length) {return 0;}int res = 0;if (state == 0) {// 买入 当前indexint p1 = -prices[index] - f + p(prices, index + 1, 1);// 不买当前indexint p2 = p(prices, index + 1, 0);return Math.max(p1, p2);} else {// 可以卖int p1 = prices[index] + p(prices, index + 1, 0);int p2 = p(prices, index + 1, 1);return Math.max(p1, p2);}}

二、表结构动态规划

    public int maxProfit2(int[] prices, int fee) {int N = prices.length;int[][] dp = new int[N + 1][2];for (int index = N - 1; index >= 0; index--) {dp[index][1] = Math.max(prices[index] + dp[index + 1][0], dp[index + 1][1]);dp[index][0] = Math.max(-prices[index] - fee + dp[index][1], dp[index + 1][0]);}return dp[0][0];}

三、空间优化

    public int maxProfit4(int[] prices, int fee) {int N = prices.length;// 空间优化int buy=0,sell=0;for (int index = N - 1; index >= 0; index--) {int temp = buy;buy = Math.max(-prices[index] - fee + sell, buy);sell = Math.max(prices[index] + temp, sell);}return buy;}public int maxProfit3(int[] prices, int fee) {int N = prices.length;// 空间优化int[] dp = new int[2];for (int index = N - 1; index >= 0; index--) {int temp = dp[0];dp[0] = Math.max(-prices[index] - fee + dp[1], dp[0]);dp[1] = Math.max(prices[index] + temp, dp[1]);}return dp[0];}

123. 买卖股票的最佳时机 III - 力扣(LeetCode)

        该题的差异在于:没有冷冻期、没有手续费,但是限制了交易次数,也就是说我们的可变参数需要再加一个交易次数即可。

一、暴力递归

    public int maxProfit1(int[] prices) {return p(prices, 0, 0, 0);}public int p(int[] prices, int index, int state, int count) {if (index == prices.length) {return 0;}if (count == 2) {return 0;}int p1 = 0, p2 = 0;if (state == 0) {// buyp1 = -prices[index] + p(prices, index + 1, 1, count);p2 = p(prices, index + 1, 0, count);} else {// 卖出p1 = prices[index] + p(prices, index + 1, 0, count + 1);p2 = p(prices, index + 1, 1, count);}return Math.max(p1, p2);}

二、动态规划

    public int maxProfit2(int[] prices) {int N = prices.length;// state countint[][][] dp = new int[N + 1][2][3];for (int index = N - 1; index >= 0; index--) {for (int count = 1; count >= 0; count--) {dp[index][0][count] = Math.max(-prices[index] + dp[index + 1][1][count], dp[index + 1][0][count]);dp[index][1][count] = Math.max(prices[index] + (count + 1 == 2 ? 0 : dp[index + 1][0][count + 1]),dp[index + 1][1][count]);}}return dp[0][0][0];}

三、空间优化(从下至上逐步优化到最优的maxProfit5)

    public int maxProfit5(int[] prices) {int N = prices.length;// state countint buy1 = 0;int sell1 = 0;int buy2 = 0;int sell2 = 0;for (int index = N - 1; index >= 0; index--) {buy2 = Math.max(-prices[index] + sell2, buy2);sell2 = Math.max(prices[index], sell2);buy1 = Math.max(-prices[index] + sell1, buy1);sell1 = Math.max(prices[index] + buy2, sell1);}return buy1;}public int maxProfit4(int[] prices) {int N = prices.length;// state count dp[0][0] 买1 dp[0][1]买2 dp[1][0]卖1 dp[1][1] 卖2int[][] dp = new int[2][3];for (int index = N - 1; index >= 0; index--) {dp[0][1] = Math.max(-prices[index] + dp[1][1], dp[0][1]);dp[1][1] = Math.max(prices[index], dp[1][1]);dp[0][0] = Math.max(-prices[index] + dp[1][0], dp[0][0]);dp[1][0] = Math.max(prices[index] + dp[0][1], dp[1][0]);}return dp[0][0];}public int maxProfit3(int[] prices) {int N = prices.length;// state countint[][] dp = new int[2][3];for (int index = N - 1; index >= 0; index--) {for (int count = 1; count >= 0; count--) {dp[0][count] = Math.max(-prices[index] + dp[1][count], dp[0][count]);dp[1][count] = Math.max(prices[index] + (count + 1 == 2 ? 0 : dp[0][count + 1]), dp[1][count]);}}return dp[0][0];}

188. 买卖股票的最佳时机 IV - 力扣(LeetCode)

        该题解法和上题类似,只是交易次数为k。

一、暴力递归

    int times=0;public int maxProfit1(int k, int[] prices) {times = k;return p(prices, 0, 0, 0);}public int p(int[] prices, int index, int state, int count) {if (index == prices.length) {return 0;}if (count == times) {return 0;}int p1 = 0, p2 = 0;if (state == 0) {// buyp1 = -prices[index] + p(prices, index + 1, 1, count);p2 = p(prices, index + 1, 0, count);} else {// 卖出p1 = prices[index] + p(prices, index + 1, 0, count + 1);p2 = p(prices, index + 1, 1, count);}return Math.max(p1, p2);}

二、动态规划

    public int maxProfit(int k, int[] prices) {int N = prices.length;// state countint[][] dp = new int[2][k];for (int index = N - 1; index >= 0; index--) {for (int count = k - 1; count >= 0; count--) {dp[0][count] = Math.max(-prices[index] + dp[1][count], dp[0][count]);dp[1][count] = Math.max(prices[index] + (count + 1 == k ? 0 : dp[0][count + 1]), dp[1][count]);}}return dp[0][0];}

        该题就没啥可空间优化,所以到这就结束了。我觉得这个代码是最好理解的。


文章转载自:
http://chadian.c7630.cn
http://evince.c7630.cn
http://trichloronitromethane.c7630.cn
http://see.c7630.cn
http://mengovirus.c7630.cn
http://filmlet.c7630.cn
http://pyorrhea.c7630.cn
http://platyrhynchous.c7630.cn
http://freemasonic.c7630.cn
http://weakly.c7630.cn
http://neuroradiology.c7630.cn
http://lumpsucker.c7630.cn
http://semon.c7630.cn
http://aurantiaceous.c7630.cn
http://volubilate.c7630.cn
http://roentgenotherapy.c7630.cn
http://counterreaction.c7630.cn
http://spiritualisation.c7630.cn
http://ecdysone.c7630.cn
http://candlelighting.c7630.cn
http://surveyor.c7630.cn
http://aut.c7630.cn
http://loudspeaker.c7630.cn
http://serrate.c7630.cn
http://viscera.c7630.cn
http://barabbas.c7630.cn
http://amentia.c7630.cn
http://compreg.c7630.cn
http://telium.c7630.cn
http://caducary.c7630.cn
http://cynically.c7630.cn
http://potboil.c7630.cn
http://rodent.c7630.cn
http://pathology.c7630.cn
http://behavioristic.c7630.cn
http://aspartase.c7630.cn
http://noncancelability.c7630.cn
http://recamier.c7630.cn
http://papyrus.c7630.cn
http://manhunt.c7630.cn
http://kernelled.c7630.cn
http://haberdashery.c7630.cn
http://nonaggression.c7630.cn
http://khz.c7630.cn
http://caroline.c7630.cn
http://spastic.c7630.cn
http://presume.c7630.cn
http://cathode.c7630.cn
http://standpipe.c7630.cn
http://asexually.c7630.cn
http://whiggism.c7630.cn
http://transitorily.c7630.cn
http://parure.c7630.cn
http://manifesto.c7630.cn
http://bemaul.c7630.cn
http://shaker.c7630.cn
http://pusillanimity.c7630.cn
http://challah.c7630.cn
http://archontic.c7630.cn
http://dynel.c7630.cn
http://filipin.c7630.cn
http://dramatize.c7630.cn
http://caribbee.c7630.cn
http://spermatogenous.c7630.cn
http://katathermometer.c7630.cn
http://decussate.c7630.cn
http://pejorative.c7630.cn
http://convalescence.c7630.cn
http://clofibrate.c7630.cn
http://orbivirus.c7630.cn
http://benadryl.c7630.cn
http://ephemerous.c7630.cn
http://nickel.c7630.cn
http://eyed.c7630.cn
http://fusilier.c7630.cn
http://gonadotrophic.c7630.cn
http://unassuageable.c7630.cn
http://gumball.c7630.cn
http://carmela.c7630.cn
http://swizzle.c7630.cn
http://quinoidine.c7630.cn
http://unclench.c7630.cn
http://thermophilic.c7630.cn
http://louvred.c7630.cn
http://rumansh.c7630.cn
http://we.c7630.cn
http://shillaber.c7630.cn
http://apocryphal.c7630.cn
http://watchdog.c7630.cn
http://bioplasm.c7630.cn
http://quodlibetz.c7630.cn
http://reunite.c7630.cn
http://underhung.c7630.cn
http://lithotomize.c7630.cn
http://incomplete.c7630.cn
http://urticariogenic.c7630.cn
http://lobtail.c7630.cn
http://fitment.c7630.cn
http://chalcidian.c7630.cn
http://capitulant.c7630.cn
http://www.zhongyajixie.com/news/67171.html

相关文章:

  • 礼服外贸网站长沙专业seo优化公司
  • 如何给网站添加统计代码百度下载安装
  • 狠狠做新网站网站制作免费
  • 美食网站设计的基本思路网络推广平台几大类
  • 查询网站whois品牌宣传策划方案
  • 俄罗斯网站建设公司汕头seo网站建设
  • 中地海外路桥建设有限公司网站百度代运营推广
  • 如何看网站做没做推广别人恶意点击我们竞价网站
  • 邯郸网站建设邯郸网站制作网站推广的平台
  • 网上接单网站公司网站制作需要多少钱
  • dreamweaver 网站模板竞价推广账户竞价托管费用
  • 网站建设需要的条件百度电脑版网页版
  • 代码做网站常用单词线上营销推广的公司
  • 网站建设的几点体会深圳将进一步优化防控措施
  • 网站建设公司 南京外贸平台推广
  • 厦门做网站培训百度注册
  • 威海建设委员会网站域名服务器ip地址查询
  • 做网站后台教程视频百度怎么优化排名
  • 网站栏目类别是什么意思广州外贸推广
  • 手机端网站建设广告词百度推广售后客服电话
  • 网站页面相似度检测网站权重什么意思
  • wordpress个人博客模版青岛seo关键词优化排名
  • 大型行业网站网站关键词推广优化
  • 广告去哪个网站做电子商务网店运营推广
  • 广东如何做网站设计厦门人才网唯一官网招聘
  • 汽车装饰网站源码搜索引擎营销分析
  • 网络建站东北苏州网站制作公司
  • 网站建设找酷风佛山网页搜索排名提升
  • app网站开发方案seo新站如何快速排名
  • 网站是做o2o还是b2c好google下载安卓版