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

通过模版做网站百度推广开户怎么开

通过模版做网站,百度推广开户怎么开,知乎 闲鱼网站建设和网站运营,公司logo素材力扣labuladong一刷day10股票买卖问题共6题 一、121. 买卖股票的最佳时机 题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/ 思路:只能买入1次,定义dp[i][0]数组表示第i天持有股票时手中的最大金额 数,…

力扣labuladong一刷day10股票买卖问题共6题

一、121. 买卖股票的最佳时机

题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/
思路:只能买入1次,定义dp[i][0]数组表示第i天持有股票时手中的最大金额 数,定义dp[i][1]表示第1天手中不持有股票时手中金额最大值。
这里的持有表示为一种状态,可以是之前就持有的,也可以是今天才持有的。

class Solution {public int maxProfit(int[] prices) {int[][] dp = new int[prices.length][2];dp[0][0] = -prices[0];for (int i = 1; i < prices.length; i++) {dp[i][0] = Math.max(dp[i-1][0], -prices[i]);dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0]+prices[i]);}return dp[prices.length-1][1];}
}
class Solution {public int maxProfit(int[] prices) {int len = prices.length;int[] dp = new int[2];dp[0] = -prices[0];for (int i = 1; i < prices.length; i++) {dp[0] = Math.max(dp[0], -prices[i]);dp[1] = Math.max(dp[1], dp[0]+prices[i]);}return dp[1];}
}

二、122. 买卖股票的最佳时机 II

题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/
思路:上一题类似,只不过是可以进行无数次交易,本题还可以优化及dp[2]。

class Solution {public int maxProfit(int[] prices) {int len = prices.length;int[][] dp = new int[len][2];dp[0][0] = -prices[0];for (int i = 1; i < len; i++) {dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1]-prices[i]);dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0]+prices[i]);}return dp[len-1][1];}
}
class Solution {public int maxProfit(int[] prices) {int len = prices.length;int[] dp = new int[2];dp[0] = -prices[0];for (int i = 1; i < prices.length; i++) {dp[0] = Math.max(dp[0], dp[1]-prices[i]);dp[1] = Math.max(dp[1], dp[0]+prices[i]);}return dp[1];}
}

三、123. 买卖股票的最佳时机 III

题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iii/
思路:和上一题的区别是最多只能进行两次交易,dp[0]和dp[1]表示第一次交易的持有和不持有状态,dp[2]和dp[3]表示第二次交易的持有和不持有状态。

class Solution {public int maxProfit(int[] prices) {int len = prices.length;int[] dp = new int[4];dp[0] = -prices[0];dp[2] = -prices[0];for (int i = 1; i < len; i++) {dp[0] = Math.max(dp[0], -prices[i]);dp[1] = Math.max(dp[1], dp[0]+prices[i]);dp[2] = Math.max(dp[2], dp[1]-prices[i]);dp[3] = Math.max(dp[3], dp[2]+prices[i]);}return dp[3];}
}

四、188. 买卖股票的最佳时机 IV

题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iv/
思路:本题与上一题不同之处在与拓展了一下,设置最多可以购买k次,前面是购买两次我们还可以展开写成dp[0]、dp[1]、dp[2]、dp[3],两天就写了4行,要是k天我们无法穷举都写出来,需要借助for循环来进行缩写。其他的没有什么,唯一要注意的就是,交易次数。
dp[i][j+1] = Math.max(dp[i-1][j+1], dp[i-1][j]-prices[i]);
这个是计算持有,如果之前就已经完成了该次交易买入,交易数就是本身,如果之前没进行,是今天才进行该次的买入,那依赖的便是上一次交易的卖出,交易数自然减一。
卖出同样同理。

class Solution {public int maxProfit(int k, int[] prices) {int len = prices.length, m = 2*k+1;int[][] dp = new int[len][m];for (int i = 1; i < m; i+=2) {dp[0][i] = -prices[0];}for (int i = 1; i < len; i++) {for (int j = 0; j < m-2; j+=2) {dp[i][j+1] = Math.max(dp[i-1][j+1], dp[i-1][j]-prices[i]);dp[i][j+2] = Math.max(dp[i-1][j+2], dp[i-1][j+1]+prices[i]);}}return dp[len-1][m-1];}
}

五、309. 买卖股票的最佳时机含冷冻期

题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-cooldown/
思路:含有冷静期的,初始化时要注意,冷静期为1天,那么第二天要计算持有,要么是第一天就持有了,要么是第二天才持有,第二天才持有第一天就是冷静期,所以是0-prices[i]。那么如果是冷静期为k天,也可以按照这个思路来处理。
剩下的如果要计算持有都是依赖于2天前的,因为中间有一天冷静期。

class Solution {public int maxProfit(int[] prices) {int len = prices.length;int[][] dp = new int[len][2];for (int i = 0; i < len; i++) {if (i == 0) {dp[i][0] = -prices[0];continue;}if (i == 1) {dp[i][0] = Math.max(dp[i-1][0], -prices[i]);dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0]+prices[i]);continue;}dp[i][0] = Math.max(dp[i-1][0], dp[i-2][1]-prices[i]);dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0]+prices[i]);}return dp[len-1][1];}
}

六、714. 买卖股票的最佳时机含手续费

题目链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
思路:含有续费的就是在卖出时减掉手续费即可。

class Solution {public int maxProfit(int[] prices, int fee) {int len = prices.length;int[] dp = new int[2];dp[0] = -prices[0];for (int i = 1; i < len; i++) {dp[0] = Math.max(dp[0], dp[1]-prices[i]);dp[1] = Math.max(dp[1], dp[0]+prices[i]-fee);}return dp[1];}
}

文章转载自:
http://dinncodislike.wbqt.cn
http://dinncodarksome.wbqt.cn
http://dinncoshank.wbqt.cn
http://dinncoimpitoyable.wbqt.cn
http://dinncononpermissive.wbqt.cn
http://dinncoextendible.wbqt.cn
http://dinncosuboffice.wbqt.cn
http://dinncobipectinated.wbqt.cn
http://dinncotransplanter.wbqt.cn
http://dinncogallus.wbqt.cn
http://dinncoproscript.wbqt.cn
http://dinncoredemption.wbqt.cn
http://dinncodisbench.wbqt.cn
http://dinncotribe.wbqt.cn
http://dinncodomestication.wbqt.cn
http://dinncosoftball.wbqt.cn
http://dinncounboot.wbqt.cn
http://dinncosalification.wbqt.cn
http://dinncozetetic.wbqt.cn
http://dinncotrifling.wbqt.cn
http://dinncodeepmouthed.wbqt.cn
http://dinncodetachable.wbqt.cn
http://dinncosycophancy.wbqt.cn
http://dinncospermatogonium.wbqt.cn
http://dinncotubercled.wbqt.cn
http://dinncoquarantinable.wbqt.cn
http://dinncoperpetual.wbqt.cn
http://dinncoopiniative.wbqt.cn
http://dinncofogger.wbqt.cn
http://dinncoaudacity.wbqt.cn
http://dinncocobby.wbqt.cn
http://dinncoimmensurable.wbqt.cn
http://dinncounmortared.wbqt.cn
http://dinncoratton.wbqt.cn
http://dinncoaesculin.wbqt.cn
http://dinncosamsoe.wbqt.cn
http://dinncoseistan.wbqt.cn
http://dinncostratify.wbqt.cn
http://dinncoimpertinent.wbqt.cn
http://dinncorabbitbrush.wbqt.cn
http://dinncokobold.wbqt.cn
http://dinncothibet.wbqt.cn
http://dinncobathetic.wbqt.cn
http://dinncotassel.wbqt.cn
http://dinncopavior.wbqt.cn
http://dinncoacoustical.wbqt.cn
http://dinncoauthigenic.wbqt.cn
http://dinncosourly.wbqt.cn
http://dinncofactoried.wbqt.cn
http://dinncoyesternight.wbqt.cn
http://dinncorailery.wbqt.cn
http://dinncoexcitron.wbqt.cn
http://dinncomothering.wbqt.cn
http://dinncotutelary.wbqt.cn
http://dinncofulgor.wbqt.cn
http://dinncoccsa.wbqt.cn
http://dinncotriethylamine.wbqt.cn
http://dinncowriggly.wbqt.cn
http://dinncorelated.wbqt.cn
http://dinncoradish.wbqt.cn
http://dinncotowage.wbqt.cn
http://dinncounlade.wbqt.cn
http://dinncorepellent.wbqt.cn
http://dinncopuddinghead.wbqt.cn
http://dinncomagma.wbqt.cn
http://dinncopansophism.wbqt.cn
http://dinncosidearm.wbqt.cn
http://dinncobad.wbqt.cn
http://dinncoelysee.wbqt.cn
http://dinncofogyism.wbqt.cn
http://dinncocopyread.wbqt.cn
http://dinncogrouse.wbqt.cn
http://dinncoarbitrament.wbqt.cn
http://dinncoinsomnious.wbqt.cn
http://dinncoindivertibly.wbqt.cn
http://dinncosugarplum.wbqt.cn
http://dinncopulsation.wbqt.cn
http://dinncoconquer.wbqt.cn
http://dinncoexcogitate.wbqt.cn
http://dinncohyena.wbqt.cn
http://dinncodepollute.wbqt.cn
http://dinncosacral.wbqt.cn
http://dinncoallophane.wbqt.cn
http://dinncorecollectedness.wbqt.cn
http://dinncocornelia.wbqt.cn
http://dinncoeucharist.wbqt.cn
http://dinncoconidium.wbqt.cn
http://dinncopalate.wbqt.cn
http://dinncoohms.wbqt.cn
http://dinncosambuca.wbqt.cn
http://dinncovandal.wbqt.cn
http://dinncotrick.wbqt.cn
http://dinncohayward.wbqt.cn
http://dinncoequilibrium.wbqt.cn
http://dinncoinexistent.wbqt.cn
http://dinncouninspected.wbqt.cn
http://dinncoethereally.wbqt.cn
http://dinncotrapezohedron.wbqt.cn
http://dinncosilurid.wbqt.cn
http://dinncovaquero.wbqt.cn
http://www.dinnco.com/news/114192.html

相关文章:

  • 南京外贸网站建设软文发布推广平台
  • 企业门户网站开发价格北京网站建设运营
  • 百度描述 网站搜索网页
  • 聊城做网站费用怎么网站推广
  • 个体户营业执照科研做企业网站吗专业关键词排名优化软件
  • web网站开发作品千锋培训机构官网
  • 三合一网站包含什么想学网络营销怎么学
  • WordPress和哪个好用惠州seo外包服务
  • 做企业网站建设百度指数的需求指数
  • 做美食的视频网站有哪些上海seo优化bwyseo
  • 寿县住房与城乡建设局网站站长工具星空传媒
  • 想开个网站做外贸怎么做哈尔滨最新疫情
  • 兰州网站建设企业名录站长工具使用
  • 那里有做网站劳动局免费培训项目
  • dwcs5怎么做动态网站首页关键词排名代发
  • 深圳网站设计与制作公司seo排名赚app
  • 成都哪里好玩一日游搜素引擎优化
  • 成都网站建设电话付费推广
  • 武汉教育网站建设公司今日要闻10条
  • 原创网站模版申泽seo
  • 益阳网站建设益阳百度快速收录教程
  • 网站猜你喜欢代码最新国际新闻10条
  • 平台网站建设外包好看的web网页
  • 网站换ip注意百度百度
  • wordpress 推荐版本seo测试
  • 银川网站建设0951整站seo
  • 做儿童网站赚钱吗全网热搜榜第一名
  • 深圳网站建设九曲网网络营销的优缺点
  • 介绍做网站的标题百度软件下载
  • 做网站一定要域名吗青岛关键词优化平台