当前位置:网站首页>Leetcode 1049. 最后一块石头的重量 II
Leetcode 1049. 最后一块石头的重量 II
2022-06-12 18:38:00 【Changersh】
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
思路
本题是说把一堆石头,两两合并,重量相抵消,问最后剩下多少的重量,其实和Leetcode 416. 分割等和子集思路相似的,只不过416是问能否完全消除。
我们直接把重量总和sum 除以二,求背包的容量,因为sum可能是奇数或者偶数,所以除二是向下取整,所以dp[tmp] <= sum - dp[tmp]
dp[i]:容量是 i 的时候装满背包的最大的重量和
所以我们要动态规划求出来,当容量为 tmp 的时候装满背包的最大的重量和是多少(即dp[tmp])
递推公式:dp[j] = max(dp[j], dp[j - stones[i]] + stones[i]) ;,一个是不装当前的石头,另一种情况是可以装进去当前的石头,取dp[j - stones[i]]加上当前石头的重量,然后比大小,取大的。
初始化:因为没有负的质量,所以全部初始化为0即可,然后看dp[0],质量为0的时候,也是初始化为 0。
遍历顺序:先遍历物品,后遍历背包
代码
class Solution {
public:
// 思路跟《分割等和子集》是一样的,都是要尽可能分出来两个相等的子集
// 但是不硬性要求要相等
int lastStoneWeightII(vector<int>& stones) {
int sum = 0;
for (int i = 0; i < stones.size(); i++)
sum += stones[i];
int tmp = sum / 2; // 向下取整,还剩下sum - tmp的
int dp[tmp + 1];
for (int i = 0; i < tmp + 1; i++) dp[i] = 0;
for (int i = 0; i < stones.size(); i++) {
for (int j = tmp; j >= stones[i]; j--)
dp[j] = max(dp[j], dp[j - stones[i]] + stones[i]);
}
// 分成两堆石头,一堆dp[tmp],一堆sum - dp[tmp],并且,因为sum / 2向下取整,所以,前者一定小于等于后者
return sum - dp[tmp] - dp[tmp];
}
};
边栏推荐
- 01-复杂度
- 静态内存分配和动态内存分配小结
- Interior design style type, rendering 100 invitation code [1a12]
- Gospel of audio and video developers, rapid integration of AI dubbing capability
- 深圳3月14日起全市停工停业7天居家办公心得|社区征文
- Go init initialization function
- bilibili视频列表名字太长显示不全的解决方法
- leetcode:5289. 公平分发饼干【看数据范围 + dfs剪枝】
- VirtualLab基础实验教程-4.单缝衍射
- What is SAP support package stack
猜你喜欢

Introduction to service grid and istio - continued

To understand Devops, you must read these ten books!

GD32F4xx控制DGUS触控按键

Title 37: sorting 10 numbers

论大型政策性银行贷后,如何数字化转型 ?-亿信华辰

干货 | 一文搞定 pytest 自动化测试框架(二)

基于Halcon的螺栓螺丝部分划痕、腐蚀缺陷检测

OpenGL shadow implementation (soft shadow)

Solution to the problem that the anaconda navigator card logo cannot be opened and the card will flash back - replace the alicloud image source

Quickly copy the request in browser F12 to postman/ or generate the corresponding code of the relevant language
随机推荐
wireshark基本使用命令
Review of MySQL (VI): usage of Union and limit
279. perfect square
Analyzing mobx responsive refresh mechanism from source code
C language learning -- data storage in memory
A story on the cloud of the Centennial Olympic Games belonging to Alibaba cloud video cloud
2022.6.12-----leetcode.890
Getting started with the go language is simple: read / write lock
GD32F4xx控制DGUS 变量显示
General differences between SQL server versions released by Microsoft in different periods so far, for reference
The difference between user status and system status in CRM
每日一博 - 微服务权限一二事
VirtualLab basic experiment tutorial -6 Blazed grating
leetcode:6097. 替换字符后匹配【set记录 + 相同长度逐一查询】
Vue —— 进阶 vue-router 路由(二)(replace属性、编程式路由导航、缓存路由组件、路由的专属钩子)
实验10 Bezier曲线生成-实验提高-交互式生成B样条曲线
快速复制浏览器F12中的请求到Postman/或者生成相关语言的对应代码
Problems that the sap Spartacus e-commerce cloud UI shipping method does not display in the unit test environment
Title 66: input 3 numbers a, B, C, and output them in order of size.
看不懂Kotlin源码?从Contracts 函数说起~