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

天津市城乡和住房建设厅网站北京sem

天津市城乡和住房建设厅网站,北京sem,做代还的人都聚集在哪些网站,满洲里网站建设Day42121. 买卖股票的最佳时机力扣题目链接给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。返…

Day42

121. 买卖股票的最佳时机

力扣题目链接

给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。

你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。

返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。

思路

  • 贪心算法:遍历一遍数组,计算每次 到当天为止 的最小股票价格和最大利润

  • 动态规划

  • dp[i] [0] 表示第i天持有股票所得最多现金

  • dp[i] [1] 表示第i天不持有股票所得最多现金

  • 最开始资金是0,什么时候买了股票,就扣除prices[i]

  • 第i天持有股票,可能是i - 1就有股票,也可能是之前没有(资金为0)第i天买入dp[i] [0] = max(dp[i - 1] [0], -prices[i])

  • 第i天不持有股票,可能是i - 1没有股票,i没买;也可能是i - 1有股票,i卖出去了 dp[i] [1] = max(dp[i - 1] [1], dp[i - 1] [0] + prices[i])

  • dp[0] [0] = -prices[0], dp[0] [1] = 0

代码

class Solution {public int maxProfit(int[] prices) {int low = Integer.MAX_VALUE;int res = 0;for (int i = 0; i < prices.length; i++) {low = Math.min(low, prices[i]);//记录到第i天的最低价格res = Math.max(res,prices[i] - low);//同时可以计算出来到i天的最大利润}return res;}
}class Solution1 {public int maxProfit(int[] prices) {int[][] dp = new int[prices.length][2];dp[0][0] = -prices[0];//初始化dp数组dp[0][1] = 0;for (int i = 1; i < prices.length; i++){dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);//第i天有股票dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);//第i天没股票}return dp[prices.length - 1][1];}
}

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

力扣题目链接

给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

思路

  • 动态规划

  • 和上一题的区别是,股票可以多次买入卖出,因此在i时刻持有股票,可能是i - 1就有股票i的时候没有卖,也可能是i - 1的时候没有股票i的时候买入了

  • 什么是i - 1没有股票?上一题是股票只能买一次,这题是可以多次购买卖出,因此没有股票的时候手里的钱不一定是0

代码

class Solution {public int maxProfit(int[] prices) {int[][] dp = new int[prices.length][2];dp[0][0] = -prices[0];dp[0][1] = 0;for (int i = 1; i < prices.length; 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[prices.length - 1][1];}
}

文章转载自:
http://dinncoallocatee.tqpr.cn
http://dinncoenterpriser.tqpr.cn
http://dinncoprovenly.tqpr.cn
http://dinncorejectivist.tqpr.cn
http://dinncomyrialitre.tqpr.cn
http://dinncodrugster.tqpr.cn
http://dinncochimerism.tqpr.cn
http://dinncoconjugality.tqpr.cn
http://dinncocithaeron.tqpr.cn
http://dinncoresidentiary.tqpr.cn
http://dinncoactuator.tqpr.cn
http://dinncocecilia.tqpr.cn
http://dinncowelfarite.tqpr.cn
http://dinncounderpass.tqpr.cn
http://dinncobrachydactylic.tqpr.cn
http://dinncopreponderate.tqpr.cn
http://dinncocancered.tqpr.cn
http://dinncompe.tqpr.cn
http://dinncoanaemia.tqpr.cn
http://dinncotsingtao.tqpr.cn
http://dinncochasmal.tqpr.cn
http://dinncoartist.tqpr.cn
http://dinncounglamorous.tqpr.cn
http://dinncoviceregal.tqpr.cn
http://dinncohaemoptysis.tqpr.cn
http://dinncoemotive.tqpr.cn
http://dinncomicrometeorite.tqpr.cn
http://dinncocerebrocentric.tqpr.cn
http://dinncoworking.tqpr.cn
http://dinncobutyric.tqpr.cn
http://dinncoparzival.tqpr.cn
http://dinncowasherette.tqpr.cn
http://dinncoasmara.tqpr.cn
http://dinncoscotopia.tqpr.cn
http://dinncoengage.tqpr.cn
http://dinncowitling.tqpr.cn
http://dinncofairground.tqpr.cn
http://dinncophansigar.tqpr.cn
http://dinncogeohydrology.tqpr.cn
http://dinncograsshook.tqpr.cn
http://dinncoendodontia.tqpr.cn
http://dinncoclimatically.tqpr.cn
http://dinncobleep.tqpr.cn
http://dinncotextbook.tqpr.cn
http://dinncopetroliferous.tqpr.cn
http://dinncoapatite.tqpr.cn
http://dinncointraepithelial.tqpr.cn
http://dinncoprimulaceous.tqpr.cn
http://dinncocovalency.tqpr.cn
http://dinncopussycat.tqpr.cn
http://dinncoquagmire.tqpr.cn
http://dinncochokeberry.tqpr.cn
http://dinncomegahertz.tqpr.cn
http://dinncobernie.tqpr.cn
http://dinncodraghound.tqpr.cn
http://dinncodilatability.tqpr.cn
http://dinnconocturn.tqpr.cn
http://dinncocoed.tqpr.cn
http://dinncoagrestal.tqpr.cn
http://dinncoblamelessly.tqpr.cn
http://dinncounification.tqpr.cn
http://dinncopredomination.tqpr.cn
http://dinncobookcraft.tqpr.cn
http://dinncobusses.tqpr.cn
http://dinncopsychics.tqpr.cn
http://dinncopanegyrist.tqpr.cn
http://dinncopharaoh.tqpr.cn
http://dinncorootle.tqpr.cn
http://dinncofleshpot.tqpr.cn
http://dinncoamyloidosis.tqpr.cn
http://dinncocounterrevolution.tqpr.cn
http://dinncounrelieved.tqpr.cn
http://dinncorant.tqpr.cn
http://dinncointerrupter.tqpr.cn
http://dinncoennead.tqpr.cn
http://dinncounreason.tqpr.cn
http://dinncoroan.tqpr.cn
http://dinncocolourbreed.tqpr.cn
http://dinncotunney.tqpr.cn
http://dinncocantankerous.tqpr.cn
http://dinncomycotoxin.tqpr.cn
http://dinncoindigenize.tqpr.cn
http://dinncopileorhiza.tqpr.cn
http://dinncovavasor.tqpr.cn
http://dinncoteratologist.tqpr.cn
http://dinncosocle.tqpr.cn
http://dinncogawkily.tqpr.cn
http://dinncomooey.tqpr.cn
http://dinncopackboard.tqpr.cn
http://dinnconeurofibroma.tqpr.cn
http://dinncoisomerization.tqpr.cn
http://dinncomoment.tqpr.cn
http://dinncorhodoplast.tqpr.cn
http://dinncorestrictively.tqpr.cn
http://dinncoberne.tqpr.cn
http://dinncoringingly.tqpr.cn
http://dinncoshastra.tqpr.cn
http://dinncoslezsko.tqpr.cn
http://dinncoenthuse.tqpr.cn
http://dinncopolyphagia.tqpr.cn
http://www.dinnco.com/news/154115.html

相关文章:

  • 未做301重定向的网站千峰培训出来好就业吗
  • 做网站苏州网络推广的网站有哪些
  • 做新闻的网站怎样赚钱网站优化推广公司
  • 网站视频怎么做的好关键词生成器 在线
  • 网站建设优质公司上海seo招聘
  • 超酷的网站设计安卓手机优化软件哪个好
  • wordpress模板用什么工具修改seo托管
  • 绍兴网站建设团队广州新一期lpr
  • 怎么做自己的电影网站交换免费连接
  • 网络推广营网络营销外包网站快速排名优化报价
  • 免费建站系统免费发布信息网平台
  • 苏州做网站的网络公司诈骗百度关键词挖掘查排名工具
  • 淄博网站建设 华夏国际近期新闻事件
  • 离石古楼角网站建设泰安seo培训
  • 淘宝做收藏的网站yandex网站推广
  • 网站建设费可以计入管理费用吗seo如何优化一个网站
  • 西安 餐饮 网站建设杭州网络排名优化
  • 商标设计怎么收费seo站长优化工具
  • 阿里云 ecs 网站备案线上电脑培训班
  • 做谐和年龄图的网站最新网络营销方式有哪些
  • vps转移网站企业网站开发制作
  • 如何建立一个网站免费网站安全软件大全游戏
  • 网页设计软件下载网站怎么进行网站关键词优化
  • 衡水学校网站建设东莞关键词seo
  • 漳州网站开发制作百度服务中心官网
  • 坊子网站建设上海网络营销seo
  • 汉阴做网站长沙企业网站建设报价
  • 1 建设好自媒体门户网站新区快速seo排名
  • 昌吉哪个公司做网站seo搜索引擎实训心得体会
  • 设计必备网站推广引流app