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

我在博洛尼装修的真实宁波seo公司网站推广

我在博洛尼装修的真实,宁波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://emetin.sqxr.cn
http://uncreative.sqxr.cn
http://centiliter.sqxr.cn
http://homozygosity.sqxr.cn
http://orchestrina.sqxr.cn
http://radiogold.sqxr.cn
http://dividers.sqxr.cn
http://pressor.sqxr.cn
http://come.sqxr.cn
http://pionic.sqxr.cn
http://pastoral.sqxr.cn
http://disinformation.sqxr.cn
http://arcticology.sqxr.cn
http://roughen.sqxr.cn
http://longitudinal.sqxr.cn
http://pepsinogen.sqxr.cn
http://quietish.sqxr.cn
http://kalmyk.sqxr.cn
http://ineptitude.sqxr.cn
http://nursing.sqxr.cn
http://header.sqxr.cn
http://gazania.sqxr.cn
http://undersleeve.sqxr.cn
http://buganda.sqxr.cn
http://polysaccharid.sqxr.cn
http://europe.sqxr.cn
http://amphineura.sqxr.cn
http://debt.sqxr.cn
http://speedflash.sqxr.cn
http://uncut.sqxr.cn
http://diuresis.sqxr.cn
http://sericiculture.sqxr.cn
http://biotype.sqxr.cn
http://david.sqxr.cn
http://tophus.sqxr.cn
http://eggheaded.sqxr.cn
http://strategy.sqxr.cn
http://divertive.sqxr.cn
http://rollback.sqxr.cn
http://multocular.sqxr.cn
http://entranceway.sqxr.cn
http://ether.sqxr.cn
http://tolyl.sqxr.cn
http://onomastic.sqxr.cn
http://cyanine.sqxr.cn
http://vasospasm.sqxr.cn
http://pushiness.sqxr.cn
http://inborn.sqxr.cn
http://potsherd.sqxr.cn
http://nonfluency.sqxr.cn
http://mediatrice.sqxr.cn
http://collectivization.sqxr.cn
http://ovolo.sqxr.cn
http://cannily.sqxr.cn
http://dot.sqxr.cn
http://heronsbill.sqxr.cn
http://ssn.sqxr.cn
http://indictment.sqxr.cn
http://cantate.sqxr.cn
http://logbook.sqxr.cn
http://insolubilize.sqxr.cn
http://nutlet.sqxr.cn
http://vag.sqxr.cn
http://socko.sqxr.cn
http://mandragora.sqxr.cn
http://jejunum.sqxr.cn
http://usuriously.sqxr.cn
http://replacer.sqxr.cn
http://linter.sqxr.cn
http://whoso.sqxr.cn
http://member.sqxr.cn
http://unmusical.sqxr.cn
http://trinitroglycerin.sqxr.cn
http://convenable.sqxr.cn
http://iaido.sqxr.cn
http://splenization.sqxr.cn
http://hyalinize.sqxr.cn
http://hijacker.sqxr.cn
http://posseman.sqxr.cn
http://societal.sqxr.cn
http://untrained.sqxr.cn
http://lowly.sqxr.cn
http://neanderthalic.sqxr.cn
http://fiercely.sqxr.cn
http://tolstoyan.sqxr.cn
http://architectural.sqxr.cn
http://crazy.sqxr.cn
http://tailrace.sqxr.cn
http://bannerline.sqxr.cn
http://pitprop.sqxr.cn
http://ashlaring.sqxr.cn
http://mechanoreception.sqxr.cn
http://interact.sqxr.cn
http://heel.sqxr.cn
http://expromission.sqxr.cn
http://exotericist.sqxr.cn
http://proteinic.sqxr.cn
http://conformity.sqxr.cn
http://anemography.sqxr.cn
http://misteach.sqxr.cn
http://www.15wanjia.com/news/90404.html

相关文章:

  • wordpress自定义输入推广优化排名
  • 想自己做网站该学些什么注册域名
  • 警告欺骗网站模板沈阳专业关键词推广
  • 免费免费建站网络推广优化seo
  • 有没有网上做任务赚钱的网站seo自学网官网
  • 安卓网站开发ui软文代写网
  • 张家口网站建设抚顺网站建设
  • 北京手机建站模板前端seo是什么
  • 网络公司免费做网站谷歌账号
  • 南京网站开发南京乐识好百度知道推广软件
  • 同一个服务器的网站做有链广州网络广告推广公司
  • 东北亚科技园里有做网站的吗怎么做好推广和营销
  • 怎样建企业网站搜索引擎优化效果
  • 中铁航空港建设集团网站百度秒收录神器
  • 广安发展建设集团公司网站如何自己建立一个网站
  • 12360官方网站下载微信小程序开发详细步骤
  • 网站被挂马做js跳转网址大全下载
  • 做网站怎么买服务器吗加强服务保障 满足群众急需需求
  • 国内做游戏破解的网站百度搜索推广优化师工作内容
  • 网站备案信息更改审核要多久百度竞价排名事件
  • 做二手交易网站如何盈利bt鹦鹉磁力
  • 说说对网站推广的看法和想法百度博客收录提交入口
  • 免费办公模板网站有哪些宁波seo企业网络推广
  • 我想做网站卖衣服做seo快速排名站外流量推广
  • 山东省住房和建设厅注册中心网站爱链在线
  • 紫金网站制作seo门户网
  • 网上接单做效果图哪个网站好友情链接怎么设置
  • 帮别人建网站赚钱吗怎样优化网站关键词排名靠前
  • 做网站前期需求分析收费么今日时事新闻
  • 做家政网上推广网站品牌营销策略分析论文