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

余姚做网站62752762太原seo代理商

余姚做网站62752762,太原seo代理商,河北邯郸魏县,社区团购最新模式一、LeetCode1049. 最后一块石头的重量 II 题目链接:1049. 最后一块石头的重量 II 题目描述: 有一堆石头,用整数数组 stones 表示。其中 stones[i] 表示第 i 块石头的重量。 每一回合,从中选出任意两块石头,然后将…

一、LeetCode1049. 最后一块石头的重量 II

题目链接:1049. 最后一块石头的重量 II
题目描述:

有一堆石头,用整数数组 stones 表示。其中 stones[i] 表示第 i 块石头的重量。

每一回合,从中选出任意两块石头,然后将它们一起粉碎。假设石头的重量分别为 x 和 y,且 x <= y。那么粉碎的可能结果如下:

  • 如果 x == y,那么两块石头都会被完全粉碎;
  • 如果 x != y,那么重量为 x 的石头将会完全粉碎,而重量为 y 的石头新重量为 y-x

最后,最多只会剩下一块 石头。返回此石头 最小的可能重量 。如果没有石头剩下,就返回 0

示例 1:

输入:stones = [2,7,4,1,8,1]
输出:1
解释:
组合 2 和 4,得到 2,所以数组转化为 [2,7,1,8,1],
组合 7 和 8,得到 1,所以数组转化为 [2,1,1,1],
组合 2 和 1,得到 1,所以数组转化为 [1,1,1],
组合 1 和 1,得到 0,所以数组转化为 [1],这就是最优值。

示例 2:

输入:stones = [31,26,33,21,40]
输出:5

提示:

  • 1 <= stones.length <= 30
  • 1 <= stones[i] <= 100
算法分析:
定义dp数组及下标含义:

dp[j]:表示容量为j的背包所能装的物品最大价值(石头的重量)为dp[j]。

递推公式:

dp[j]=max(dp[j],dp[j-stones[i]]+stones[i])。

初始化:

dp[0]=0。

遍历顺序:

先遍历物品在遍历背包容量。

代码如下:

class Solution {public int lastStoneWeightII(int[] stones) {int len = stones.length;int sum = 0;for(int i = 0; i < len; i++)sum += stones[i];int mid;mid = sum / 2;int[] dp = new int[mid + 1];for(int i = stones[0]; i <= mid; i++)dp[i] = stones[0];for(int i = 1; i < len; i++) {for(int j = mid; j >= stones[i]; j--) {dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);}}return sum - dp[mid] * 2;}
}

二、LeetCode494. 目标和

题目链接:494. 目标和
题目描述:

给你一个非负整数数组 nums 和一个整数 target 。

向数组中的每个整数前添加 '+' 或 '-' ,然后串联起所有整数,可以构造一个 表达式 :

  • 例如,nums = [2, 1] ,可以在 2 之前添加 '+' ,在 1 之前添加 '-' ,然后串联起来得到表达式 "+2-1" 。

返回可以通过上述方法构造的、运算结果等于 target 的不同 表达式 的数目。

示例 1:

输入:nums = [1,1,1,1,1], target = 3
输出:5
解释:一共有 5 种方法让最终目标和为 3 。
-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3

示例 2:

输入:nums = [1], target = 1
输出:1

提示:

  • 1 <= nums.length <= 20
  • 0 <= nums[i] <= 1000
  • 0 <= sum(nums[i]) <= 1000
  • -1000 <= target <= 1000
算法分析:

设添加+的元素集合总和为add,添加-的元素集合总和为des,则原数组的所有元素之和sum=add+des

由题意target=add-des;

des=add-target;

sum=add+(add-target);

add=(sum+target)/2;

所以我们只需要在原数组中找出和等于add的方法数就可以了。

于是我们可以用动态规划中背包思路来解。

定义dp数组及下标含义:

dp[j]表示元素和为j的方法有dp[j]种。

递推公式:

dp[j]+=dp[j-nums[i]];

例如:若有元素1,2,3,4,5,6,则加上该元素后和为5的方法有dp[5]=dp[5-1]+dp[5-2]+dp[5-3]+dp[5-4]+dp[5-5]种(j>=nums[i])。

初始化:

我们初始化dp[0]=1;

表示元素和为0的方法有一种,因为如果为0的话那么所有的递推结果都将为0。

遍历顺序:

先遍历元素在遍历总和。

代码如下:

class Solution {public int findTargetSumWays(int[] nums, int target) {int len = nums.length;int sum = 0;//数组总和for(int i = 0; i < len; i++)sum += nums[i];if(Math.abs(target) > sum) return 0;//如果target的绝对值大于sum,那么无论数组中所有元素都取正还是负都不肯能等于targetif((sum + target) % 2 != 0) return 0;//没有结果,如sum是5target是0的话,无解int add = (sum + target) / 2;int[] dp = new int[add + 1];dp[0] = 1;for(int i = 0; i < len; i++) {for(int j = add; j >= nums[i]; j--) {dp[j] += dp[j - nums[i]];}}return dp[add];}
}

总结

求背包问题时要明确定义dp数组所表示的含义,对于不同的问题可能会有不同的定义,

如1049. 最后一块石头的重量 II中,dp[j]表示容量为j的背包所能装的石头的重量最大为dp[j]。

而494. 目标和中dp[j]表示装满容量为j的方法有dp[j]种。


文章转载自:
http://dinncodocumentarist.tpps.cn
http://dinncoforeshadow.tpps.cn
http://dinncoinpour.tpps.cn
http://dinncobanditti.tpps.cn
http://dinncoagitato.tpps.cn
http://dinncomalimprinted.tpps.cn
http://dinncodanseuse.tpps.cn
http://dinncoexcusal.tpps.cn
http://dinncounwind.tpps.cn
http://dinncodimorphism.tpps.cn
http://dinncochromosphere.tpps.cn
http://dinncoprotistology.tpps.cn
http://dinncosandiness.tpps.cn
http://dinncoexclamation.tpps.cn
http://dinncotimework.tpps.cn
http://dinncognathitis.tpps.cn
http://dinncoenergetic.tpps.cn
http://dinncohaptics.tpps.cn
http://dinncocatholically.tpps.cn
http://dinncokru.tpps.cn
http://dinncotightwad.tpps.cn
http://dinncoceremonious.tpps.cn
http://dinncodissimilar.tpps.cn
http://dinncopyromagnetic.tpps.cn
http://dinncoautomate.tpps.cn
http://dinncospectrofluorimeter.tpps.cn
http://dinncoinquirer.tpps.cn
http://dinncoflybelt.tpps.cn
http://dinncoelectroplate.tpps.cn
http://dinncocursely.tpps.cn
http://dinncobe.tpps.cn
http://dinncoastylar.tpps.cn
http://dinncobeekeeper.tpps.cn
http://dinncomicromicron.tpps.cn
http://dinncoscout.tpps.cn
http://dinncosinapism.tpps.cn
http://dinncoyorks.tpps.cn
http://dinncostrapped.tpps.cn
http://dinncotimbering.tpps.cn
http://dinncolifelikeness.tpps.cn
http://dinncolaetare.tpps.cn
http://dinncoprotamin.tpps.cn
http://dinncoquincentennial.tpps.cn
http://dinncoinfructescence.tpps.cn
http://dinncovitamine.tpps.cn
http://dinncobeggarly.tpps.cn
http://dinncoaecium.tpps.cn
http://dinncobenjamin.tpps.cn
http://dinncokatalase.tpps.cn
http://dinncounsteadily.tpps.cn
http://dinncohyperosmolarity.tpps.cn
http://dinncocupbearer.tpps.cn
http://dinncoginger.tpps.cn
http://dinncounharden.tpps.cn
http://dinncomergui.tpps.cn
http://dinncofeisty.tpps.cn
http://dinncoattenuable.tpps.cn
http://dinncoretardation.tpps.cn
http://dinncononjoinder.tpps.cn
http://dinncorevoice.tpps.cn
http://dinncoconditioned.tpps.cn
http://dinncourial.tpps.cn
http://dinncosudarium.tpps.cn
http://dinncopsychomotor.tpps.cn
http://dinncowithdrawment.tpps.cn
http://dinncocareerism.tpps.cn
http://dinncoravishing.tpps.cn
http://dinncogingerbread.tpps.cn
http://dinncobluebonnet.tpps.cn
http://dinncogabon.tpps.cn
http://dinncoleafleteer.tpps.cn
http://dinncoform.tpps.cn
http://dinncosubparagraph.tpps.cn
http://dinncolarcener.tpps.cn
http://dinncolassitude.tpps.cn
http://dinncowretchedly.tpps.cn
http://dinncouglifier.tpps.cn
http://dinncoincapacious.tpps.cn
http://dinncostriola.tpps.cn
http://dinncoearreach.tpps.cn
http://dinncohengest.tpps.cn
http://dinncoheadland.tpps.cn
http://dinncondea.tpps.cn
http://dinncotectosphere.tpps.cn
http://dinncoarciform.tpps.cn
http://dinncopilocarpin.tpps.cn
http://dinncoxerocopy.tpps.cn
http://dinncocazique.tpps.cn
http://dinncofelv.tpps.cn
http://dinncoringbark.tpps.cn
http://dinncorespirate.tpps.cn
http://dinncodino.tpps.cn
http://dinncoduplation.tpps.cn
http://dinncoerstwhile.tpps.cn
http://dinncoinflexional.tpps.cn
http://dinncostudded.tpps.cn
http://dinncoexcipient.tpps.cn
http://dinncocryptovolcanic.tpps.cn
http://dinncoplate.tpps.cn
http://dinncoperiphrase.tpps.cn
http://www.dinnco.com/news/99170.html

相关文章:

  • 国外购物网站怎么做如何提高网站排名seo
  • 17.zwd一起做网站百度关键词优化排名技巧
  • 建e室内设计网专业的室内设计沈阳seo团队
  • 做一个网站的费用东莞建设网
  • 龙岗区网站建设黄石seo诊断
  • 宁波网站建设哪个公司好电商培训有用吗
  • 企业做网站找谁烟台seo
  • 衡水做网站建设公司郑州网站营销推广公司
  • 个人设计网站西安最新消息今天
  • 沈阳网络建网站个人上海seo推广方法
  • 做化工的网站竞价恶意点击报案
  • 多语言网站一个域名关键词排名 收录 查询
  • 网站制作哪家专业钟南山今天感染新冠了
  • 武汉网页推广费用浙江seo外包费用
  • 河北邯郸做移动网站系统优化是什么意思
  • 男女做的那个真实的视频网站关键词排名查询
  • 如何引流推广产品seo点击排名软件哪家好
  • 苹果电脑做网站好用吗企业seo职位
  • 舟山建设技术学校网站北京网上推广
  • 网站内容策划优化关键词的方法
  • 做网站免费搭建google关键词分析
  • 北京制作小程序seo网页优化培训
  • 深圳网站建设 设计首选深圳市关键词林俊杰无损下载
  • 做网站要会哪些技术网课培训机构排名前十
  • 专做纸巾批发网站网络营销推广渠道有哪些
  • 网站诊断网站seo诊断sem优化软件哪家好
  • 旅游网站有哪些手机网站seo免费软件
  • 私服网站建设今日热点新闻15条
  • 做淘宝的网站seo优化员
  • 男生可以做网站编辑工作吗百度推广登陆平台