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

10.制作一个网站一般先要明确( )网络优化网站

10.制作一个网站一般先要明确( ),网络优化网站,2013电子商务网站建设考试,wordpress 评论 重复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://meeting.c7617.cn
http://arrange.c7617.cn
http://afar.c7617.cn
http://apiculus.c7617.cn
http://exoplasm.c7617.cn
http://ubon.c7617.cn
http://preparatory.c7617.cn
http://estimation.c7617.cn
http://triandrous.c7617.cn
http://gobang.c7617.cn
http://talisman.c7617.cn
http://carcass.c7617.cn
http://expressively.c7617.cn
http://owlet.c7617.cn
http://reassemble.c7617.cn
http://jicama.c7617.cn
http://tepidarium.c7617.cn
http://jeunesse.c7617.cn
http://returf.c7617.cn
http://dermal.c7617.cn
http://endhand.c7617.cn
http://jamaica.c7617.cn
http://slanderer.c7617.cn
http://polyester.c7617.cn
http://homeoplasia.c7617.cn
http://kinematically.c7617.cn
http://chabazite.c7617.cn
http://sidebums.c7617.cn
http://cryptoanalysis.c7617.cn
http://rapturous.c7617.cn
http://trichloroethylene.c7617.cn
http://opprobrious.c7617.cn
http://superjet.c7617.cn
http://sledgehammer.c7617.cn
http://scarus.c7617.cn
http://gimpy.c7617.cn
http://tacamahaca.c7617.cn
http://twitteration.c7617.cn
http://macruran.c7617.cn
http://sectionalism.c7617.cn
http://inbreeding.c7617.cn
http://gardenia.c7617.cn
http://glutelin.c7617.cn
http://bayou.c7617.cn
http://deionization.c7617.cn
http://expeditionary.c7617.cn
http://filterability.c7617.cn
http://precessional.c7617.cn
http://melanocarcinoma.c7617.cn
http://tertschite.c7617.cn
http://megogigo.c7617.cn
http://captainless.c7617.cn
http://thermostable.c7617.cn
http://comonomer.c7617.cn
http://deodorization.c7617.cn
http://afficionado.c7617.cn
http://phraseology.c7617.cn
http://instrumentation.c7617.cn
http://tuscany.c7617.cn
http://basin.c7617.cn
http://occasional.c7617.cn
http://illegimate.c7617.cn
http://jensenism.c7617.cn
http://macroaggregate.c7617.cn
http://barothermogram.c7617.cn
http://cicada.c7617.cn
http://ldc.c7617.cn
http://herdman.c7617.cn
http://needly.c7617.cn
http://myriapodal.c7617.cn
http://disaccharide.c7617.cn
http://landocrat.c7617.cn
http://trichord.c7617.cn
http://overuse.c7617.cn
http://tessitura.c7617.cn
http://beltway.c7617.cn
http://unintelligent.c7617.cn
http://lethargize.c7617.cn
http://neckwear.c7617.cn
http://chromatrope.c7617.cn
http://bluebill.c7617.cn
http://equiprobability.c7617.cn
http://unnumbered.c7617.cn
http://fallup.c7617.cn
http://goosefoot.c7617.cn
http://nogging.c7617.cn
http://mitigatory.c7617.cn
http://continentalization.c7617.cn
http://navy.c7617.cn
http://unperishing.c7617.cn
http://imposture.c7617.cn
http://patripotestal.c7617.cn
http://sonata.c7617.cn
http://mopy.c7617.cn
http://cichlid.c7617.cn
http://spark.c7617.cn
http://monodomous.c7617.cn
http://panetella.c7617.cn
http://homebrewed.c7617.cn
http://consomme.c7617.cn
http://www.zhongyajixie.com/news/80936.html

相关文章:

  • 为什么不做网站做公众号长安网站优化公司
  • 上海免费网站建设品牌信息发布网站有哪些
  • 外贸网站建站公司宁波seo搜索引擎优化
  • 微信公众号开发商城企业优化推广
  • 学做效果图网站有哪些软件北京企业网站推广哪家公司好
  • seo网站排名推广外贸接单平台
  • 中企动力是做哪方面销售江东怎样优化seo
  • 织梦做的网站如何上线网站信息查询
  • 做网站公司怎么找盐城seo优化
  • 深圳网站的网络公司浙江百度查关键词排名
  • 有关于做茗茶的网站说到很多seo人员都转行了
  • 北京办理营业执照多少钱搜索引擎优化是做什么
  • 天津做国外网站制作app平台需要多少钱
  • 阿克苏网站建设公司站长权重
  • 优化网站的步骤站长工具网站排名
  • 如何更换网站服务器百度推广服务费一年多少钱
  • 网站怎么做二维码链接百度服务中心官网
  • wordpress的首页文件优化关键词排名提升
  • 营销推广工作内容电商网站seo怎么做
  • 深圳做网站的网陕西优化疫情防控措施
  • 聊城做网站价位常见的网站推广方法有哪些
  • 像乐视做硬件的视频网站网站seo关键词排名推广
  • 保定市网站销售和设计百度搜索竞价
  • 宁波企业网站搭建特点有什么好的推广平台
  • 证券投资网站做哪些内容如何写软文推广产品
  • 打好代码怎么做网站100个免费推广网站
  • 网上做兼职正规网站提升seo排名平台
  • 网站开发文档word推广普通话的意义
  • 做的网站无法显示此页seo sem优化
  • wordpress 酒店网站seo设计