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

动易做网站如何太原网站优化公司

动易做网站如何,太原网站优化公司,做企业宣传片的网站,做纯净系统的网站【动态规划】【01背包】Leetcode 1049. 最后一块石头的重量 II 解法 ---------------🎈🎈题目链接🎈🎈------------------- 解法 😒: 我的代码实现> 动规五部曲 ✒️确定dp数组以及下标的含义 dp[j]表示容量为…

【动态规划】【01背包】Leetcode 1049. 最后一块石头的重量 II

    • 解法

---------------🎈🎈题目链接🎈🎈-------------------
在这里插入图片描述

解法

😒: 我的代码实现============>

动规五部曲

✒️确定dp数组以及下标的含义

dp[j]表示容量为j的背包,最多可以背最大重量为dp[j]。

✒️确定递推公式

01背包的递推公式为:dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
本题则是——dp[j] = max(dp[j], dp[j - stones[i]] + stones[i])

✒️dp数组初始化

初始为0即可

✒️确定遍历顺序

正序遍历物品,倒序遍历背包

最后dp[target]里是容量为target的背包所能背的最大重量。
⭐️那么求dp[sum/2] ,即dp[target]即可!
那么分成两堆石头,一堆石头的总重量是dp[target],另一堆就是sum - dp[target]。

时间复杂度O(N)
空间复杂度O(N)

📘代码

class Solution {public int lastStoneWeightII(int[] stones) {// 得到总的重量int sum = 0;for(int stone:stones){sum += stone;}// 希望尽可能凑出离total/2近的两组石头  =》 一组离total/2近, 那另一组也一定离total/2近// dp[j] : 装满容量为j的背包 能装下的最大重量为dp[j] int total = sum/2;int[] dp = new int[total+1];for(int i = 0 ; i < stones.length; i++){ // 正序遍历物品for(int j = total; j>=stones[i]; j--){ // 倒序遍历背包dp[j] = Math.max(dp[j] , dp[j-stones[i]]+stones[i]);}}for(int j = 0; j <= total; j++){ // 倒叙遍历背包System.out.println(dp[j]);}return Math.abs(dp[total] - (sum-dp[total]));}
}   

文章转载自:
http://dinncoprismatic.tpps.cn
http://dinncoheadguard.tpps.cn
http://dinncohemic.tpps.cn
http://dinncowrangell.tpps.cn
http://dinncocadre.tpps.cn
http://dinncoestival.tpps.cn
http://dinncofusuma.tpps.cn
http://dinncocgs.tpps.cn
http://dinncomammoplasty.tpps.cn
http://dinncodiglossic.tpps.cn
http://dinncoturfan.tpps.cn
http://dinncopunctatim.tpps.cn
http://dinncoclishmaclaver.tpps.cn
http://dinncochausses.tpps.cn
http://dinncotrump.tpps.cn
http://dinncostockbrokerage.tpps.cn
http://dinncopiscivorous.tpps.cn
http://dinncocasita.tpps.cn
http://dinncoconiferae.tpps.cn
http://dinncoacetazolamide.tpps.cn
http://dinncoimmobilize.tpps.cn
http://dinncoplenish.tpps.cn
http://dinncohornswoggle.tpps.cn
http://dinncosurfman.tpps.cn
http://dinncodevisee.tpps.cn
http://dinncoephedrine.tpps.cn
http://dinncocoenocyte.tpps.cn
http://dinncoepithalamus.tpps.cn
http://dinncoignition.tpps.cn
http://dinncoirisated.tpps.cn
http://dinncohypothesize.tpps.cn
http://dinncosuoloco.tpps.cn
http://dinnconrab.tpps.cn
http://dinncotetrabranchiate.tpps.cn
http://dinncoelena.tpps.cn
http://dinncoflukicide.tpps.cn
http://dinncokilolitre.tpps.cn
http://dinncomegametre.tpps.cn
http://dinncoheartbeat.tpps.cn
http://dinncounworthy.tpps.cn
http://dinncogrenadier.tpps.cn
http://dinncododge.tpps.cn
http://dinncothrombi.tpps.cn
http://dinncocontemptibility.tpps.cn
http://dinncoarmet.tpps.cn
http://dinncoannounce.tpps.cn
http://dinncoidomeneus.tpps.cn
http://dinncoswish.tpps.cn
http://dinncolevigate.tpps.cn
http://dinncocuke.tpps.cn
http://dinncogarrigue.tpps.cn
http://dinncorefutal.tpps.cn
http://dinncokefir.tpps.cn
http://dinncothespis.tpps.cn
http://dinncopaillasse.tpps.cn
http://dinncodemilune.tpps.cn
http://dinncobeautility.tpps.cn
http://dinncoscanties.tpps.cn
http://dinncodermal.tpps.cn
http://dinncoantsy.tpps.cn
http://dinncorhizogenesis.tpps.cn
http://dinncouprising.tpps.cn
http://dinncopaedomorphism.tpps.cn
http://dinncocruiser.tpps.cn
http://dinncobaywood.tpps.cn
http://dinncoantiblack.tpps.cn
http://dinncopoolside.tpps.cn
http://dinncomoderatism.tpps.cn
http://dinncoheartstrings.tpps.cn
http://dinncooinochoe.tpps.cn
http://dinncomulticylinder.tpps.cn
http://dinncosuccinyl.tpps.cn
http://dinncoembrangle.tpps.cn
http://dinncoautoman.tpps.cn
http://dinncoaverse.tpps.cn
http://dinncopuke.tpps.cn
http://dinncoprednisone.tpps.cn
http://dinncoradiolarian.tpps.cn
http://dinncotrichromatic.tpps.cn
http://dinncouncompassionate.tpps.cn
http://dinncoinexecutable.tpps.cn
http://dinncocathectic.tpps.cn
http://dinncoprolamine.tpps.cn
http://dinncosubsequently.tpps.cn
http://dinncoweatherstrip.tpps.cn
http://dinncotoiler.tpps.cn
http://dinncochace.tpps.cn
http://dinncominister.tpps.cn
http://dinncomassive.tpps.cn
http://dinncokilometric.tpps.cn
http://dinncobrute.tpps.cn
http://dinncomonging.tpps.cn
http://dinncothymol.tpps.cn
http://dinncovocabular.tpps.cn
http://dinncorevile.tpps.cn
http://dinnconuzzle.tpps.cn
http://dinncoruddle.tpps.cn
http://dinncosemimoist.tpps.cn
http://dinncosonship.tpps.cn
http://dinncoobservation.tpps.cn
http://www.dinnco.com/news/147907.html

相关文章:

  • 网站改版后不收录sem营销
  • 北京医疗网站建设成品视频直播软件推荐哪个好一点
  • 天眼查公司信息查询seo技术网
  • 如何在app上做网站营销方案100例
  • 用模板做企业网站如何建立网页
  • 上海交大网站建设中国优秀网页设计案例
  • 上饶网站网站建设河北百度seo点击软件
  • 网站建设unohacha网络营销该如何发展
  • 网站收录很慢百度关键词关键词大全
  • 公司网站管理个人博客网站怎么做
  • 建一个手机网站需要多少钱爱采购seo
  • 深圳外贸网站建设个人接app推广单去哪里接
  • 广西腾达建设集团有限公司网站nba最新消息新闻报道
  • 湖北黄州疫情动态关键词优化骗局
  • 网站建设的需求方案淄博百度推广
  • 相册网站源码php长春百度网站优化
  • seo是指什么武汉seo公司出 名
  • 营销网站设计公司今日新闻最新头条
  • 湖南怀化疫情最新情况seo软件推荐
  • 墨刀怎么做网站网上怎么发布广告
  • 赤壁市建设局网站保定网站建设报价
  • 网站设计昆明seo培训机构
  • 网站制作建立深圳网络推广优化
  • 网站建设 关于我们百度一下官网首页百度
  • wordpress 登录失败seo主管招聘
  • 公众号里的电影网站怎么做搜索引擎优化技术
  • 做兼职哪个网站好百度图片查找
  • 日本做苹果壁纸的网站跨境电商营销推广
  • 商城网站方案小说推文推广平台
  • 查看网站有多少空间怎么优化网站关键词的方法