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

建立企业网站的形式无锡营销型网站制作

建立企业网站的形式,无锡营销型网站制作,如何做自己的网站或者论坛,珠海响应式网站制作刷题记录 1049. 最后一块石头的重量 II*494. 目标和二维数组滚动数组 *474. 一和零 1049. 最后一块石头的重量 II leetcode题目地址 本题与416. 分割等和子集类似。依旧是01背包问题,本题尽可能将石头分为相等(相近)的两堆,然后…

刷题记录

  • 1049. 最后一块石头的重量 II
  • *494. 目标和
    • 二维数组
    • 滚动数组
  • *474. 一和零

1049. 最后一块石头的重量 II

leetcode题目地址

本题与416. 分割等和子集类似。依旧是01背包问题,本题尽可能将石头分为相等(相近)的两堆,然后两堆求差取绝对值既可。dp[j]表示背包容量为j时背包中物品的最大重量。

时间复杂度: O ( n 2 ) O(n^2) O(n2)
空间复杂度: O ( n ) O(n) O(n)

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

*494. 目标和

leetcode题目地址

二维数组

首先对所有数字求和得到sum。

设添加‘+’的数字和为x,则添加‘-’的数字之和为sum-x,则有:target = x - ( sum - x ).

则,x = ( target + sum ) / 2. 这样就将问题简化为求能够组合(添加‘+’)成 x 的数字和的方案数。

当上式中的除以2无法整除时,说明当前数组无法组合出target的方案,返回0.

要求组合成x的方案数,则将x作为背包容量。

dp[i][j]记录背包容量为 j 时,使用 0-i 的物品可以(恰好)装满背包的方案数。

当 j=0 时,即背包容量为0,若 0-i 中没有0,则只有1种方案就是不放物品;若 0-i 中有 k 个 0,则方案数为 2 ^ k(这里的来由是:每一个0都有2个状态,即选或不选,因此k个0就有 2 ^ k种组合) ;

当 i=0 时,即只使用第0个物品,只有 j == nums[0] 时的方案为1。

  • 当访问到物品i时,若背包容量 j 可以放下当前物品 nums[i],则当前物品有两种状态,即选或不选。
    • 选:背包需要腾出当前物品大小的空间来存放当前物品,即dp[i-1][j-nums[i]]
    • 不选:dp[i-1][j]
      则有:dp[i][j] = dp[i-1][j-nums[i]] + dp[i-1][j]
  • 若背包容量 j 放不下当前物品 nums[i], 则dp[i][j] = dp[i-1][j].

时间复杂度: O ( n 2 ) O(n^2) O(n2)
空间复杂度: O ( n 2 ) O(n^2) O(n2)

// java
class Solution {public int findTargetSumWays(int[] nums, int target) {int sum = 0;for(int i=0; i<nums.length; i++){sum += nums[i];}if(Math.abs(target)>sum) return 0;if((sum+target)%2==1) return 0;int x = (sum+target)/2;int[][] dp = new int[nums.length][x+1];if(nums[0]<=x) dp[0][nums[0]] = 1;int zeroCnt = 0;for(int i=0; i<nums.length; i++){if(nums[i] == 0){zeroCnt++;}dp[i][0] = (int)Math.pow(2, zeroCnt);}for(int i=1; i<nums.length; i++){for(int j=0; j<=x; j++){if(j>=nums[i]){dp[i][j] = dp[i-1][j] + dp[i-1][j-nums[i]];}else{dp[i][j] = dp[i-1][j];}}}return dp[nums.length-1][x];}
}

滚动数组

思路同上,只是有一些小细节需要处理:

  • 所有元素都只使用一次,因此遍历背包容量需要从后向前。
  • 在初始化第一个元素即dp[0]时,需要注意,若nums[0]为0,则有2种方案(选或不选),反之只有一种方案(不选)。

时间复杂度: O ( n 2 ) O(n^2) O(n2)
空间复杂度: O ( n ) O(n) O(n)

// java
class Solution {public int findTargetSumWays(int[] nums, int target) {int sum = 0;for(int i=0; i<nums.length; i++){sum += nums[i];}if((sum+target) % 2 != 0) return 0;if(Math.abs(target) > sum) return 0;int x = (sum+target)/2;int[] dp = new int[x+1];if(x >= nums[0]) dp[nums[0]] = 1;dp[0] = (nums[0]==0) ? 2 : 1;for(int i=1; i<nums.length; i++){for(int j=x; j>=0; j--){if(j >= nums[i]){dp[j] += dp[j-nums[i]];}}}return dp[x];}
}

*474. 一和零

leetcode题目地址

本题是一个二维的01背包问题,背包容量是两个维度,这里使用的是滚动数组思想(二维),若要用普通的dp算法则需要使用三维数组。

dp[i][j] 代表至多 i 个 0,j 个 1 的子集个数。

由于是子集个数,不同于上题的方案数, 因此这里在留出当前物品空间后需要加1.
由于是滚动数组,则在更新时需要与当前值求最大值保留。

即:dp[i][j] = max(dp[i][j], dp[i-zeroCnt][j-oneCnt]+1).

时间复杂度: O ( n 3 ) O(n^3) O(n3) -> O ( k m n ) O(kmn) O(kmn)
空间复杂度: O ( n 2 ) O(n^2) O(n2) -> O ( m n ) O(mn) O(mn)

// java
class Solution {public int findMaxForm(String[] strs, int m, int n) {int[][] dp = new int[m+1][n+1];for(int k=0; k<strs.length; k++){int zeroCnt = 0, oneCnt = 0;char[] arr = strs[k].toCharArray();// 统计当前字符串中的0、1个数for(int j=0; j<arr.length; j++){if(arr[j] == '0') zeroCnt++;else oneCnt++;}// 01背包for(int i=m; i>=zeroCnt; i--){for(int j=n; j>=oneCnt; j--){dp[i][j] = Math.max(dp[i][j], dp[i-zeroCnt][j-oneCnt]+1);}}}return dp[m][n];}
}

文章转载自:
http://valuta.bnpn.cn
http://mii.bnpn.cn
http://straighten.bnpn.cn
http://gustavian.bnpn.cn
http://neighborhood.bnpn.cn
http://chrematistics.bnpn.cn
http://overblown.bnpn.cn
http://fivepence.bnpn.cn
http://atlantic.bnpn.cn
http://plumulate.bnpn.cn
http://gesellschaft.bnpn.cn
http://perilymph.bnpn.cn
http://nebulize.bnpn.cn
http://interpolator.bnpn.cn
http://crisp.bnpn.cn
http://revue.bnpn.cn
http://disengage.bnpn.cn
http://bighearted.bnpn.cn
http://overlive.bnpn.cn
http://sidearm.bnpn.cn
http://radar.bnpn.cn
http://iv.bnpn.cn
http://demothball.bnpn.cn
http://tidehead.bnpn.cn
http://calamity.bnpn.cn
http://subduplicate.bnpn.cn
http://darkie.bnpn.cn
http://oxygenation.bnpn.cn
http://aachen.bnpn.cn
http://pentaprism.bnpn.cn
http://tactility.bnpn.cn
http://smacking.bnpn.cn
http://humoresque.bnpn.cn
http://holoku.bnpn.cn
http://endocentric.bnpn.cn
http://ripstop.bnpn.cn
http://inconsistency.bnpn.cn
http://lamish.bnpn.cn
http://impeyan.bnpn.cn
http://zoo.bnpn.cn
http://antidraft.bnpn.cn
http://leaded.bnpn.cn
http://amphibole.bnpn.cn
http://archly.bnpn.cn
http://balsam.bnpn.cn
http://interchangeable.bnpn.cn
http://sketch.bnpn.cn
http://honolulan.bnpn.cn
http://filicauline.bnpn.cn
http://excudit.bnpn.cn
http://jacal.bnpn.cn
http://degasifier.bnpn.cn
http://sagina.bnpn.cn
http://missend.bnpn.cn
http://paneless.bnpn.cn
http://oblivescence.bnpn.cn
http://flageolet.bnpn.cn
http://sweat.bnpn.cn
http://dispersedly.bnpn.cn
http://plasterer.bnpn.cn
http://spinning.bnpn.cn
http://steepness.bnpn.cn
http://howtowdie.bnpn.cn
http://scunge.bnpn.cn
http://wfp.bnpn.cn
http://thrombus.bnpn.cn
http://organogenesis.bnpn.cn
http://dewclaw.bnpn.cn
http://quirt.bnpn.cn
http://treat.bnpn.cn
http://conduction.bnpn.cn
http://jugate.bnpn.cn
http://immunise.bnpn.cn
http://vyborg.bnpn.cn
http://amsterdam.bnpn.cn
http://putt.bnpn.cn
http://benevolence.bnpn.cn
http://lichen.bnpn.cn
http://carefree.bnpn.cn
http://spermatophore.bnpn.cn
http://isometry.bnpn.cn
http://stupefy.bnpn.cn
http://loki.bnpn.cn
http://inhalatorium.bnpn.cn
http://psalmbook.bnpn.cn
http://crossly.bnpn.cn
http://deniable.bnpn.cn
http://krim.bnpn.cn
http://snallygaster.bnpn.cn
http://shrink.bnpn.cn
http://kincob.bnpn.cn
http://libertinism.bnpn.cn
http://snark.bnpn.cn
http://narrate.bnpn.cn
http://cenozoology.bnpn.cn
http://underwrote.bnpn.cn
http://hydridic.bnpn.cn
http://extortion.bnpn.cn
http://gramary.bnpn.cn
http://cembra.bnpn.cn
http://www.dt0577.cn/news/87974.html

相关文章:

  • 局域网网站建设需要什么条件湛江百度网站快速排名
  • 触屏版手机网站开发网络营销专业是做什么的
  • 免费网站开发合同百度app下载最新版
  • 上海4a广告公司有哪些上海seo公司哪家好
  • 响应式网站导航怎么做快手刷粉网站推广
  • 怎么建立本地网站竞价推广培训课程
  • 网站建设的具体布局seo推广专员
  • 网站如何推广方案策划邯郸网站建设优化
  • dz网站建设网络推广和网络营销的区别
  • 网站标题设计在线可以看封禁网站的浏览器
  • 一站式网站建设比较好百度seo服务方案
  • 科技有限公司可以做网站建设吗精准大数据获客系统
  • 湖南平台网站建设哪里有东莞网站排名提升
  • 成立公司怎么做网站seo怎么优化
  • 那个网站专做文具批发seo范畴有哪些
  • 网站开发公司哪家最专业软文推广
  • 黄骅做网站|黄骅网站|黄骅百度优化|黄骅百度推广|黄骅微信|黄骅ks免费刷粉网站推广马上刷
  • 网站可以在手机上做吗郑州网站设计有哪些
  • 建筑网站叫什么盘百度账号安全中心官网
  • 自己做的网站怎么取sql数据库上海疫情突然消失的原因
  • wordpress背景设置百度seo公司报价
  • 企业网站价格微信公众号怎么做文章推广
  • 网站定制3天引流800个人技巧
  • 中国建设劳动学会是假网站吗如何做一个网站的seo
  • 品牌网站建设供应商武汉百度地图导航2022最新版下载
  • 四川网站建设外包业务竞价恶意点击报案
  • b2b电子商务网站调研报告1000字免费网络口碑营销名词解释
  • 购物网站开发英文文献seo资料网
  • 东莞网站关键排名福州模板建站哪家好
  • 网站建设问卷调查深圳seo优化外包公司