当前位置:网站首页>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];
}
};
边栏推荐
- Summary of static memory allocation and dynamic memory allocation
- 279. perfect square
- 用grep awk提取字符串
- SCI Writing - Results
- 2022.6.12-----leetcode.890
- Random talk about redis source code 90
- Pytest automated testing framework (II)
- Self made calculator (1 realized by Boolean logic operation unit and control unit programming)
- Extracting strings with grep awk
- Comparison of disk mapping tools for network disk and object cloud storage management
猜你喜欢

Review of MySQL (VI): usage of Union and limit

Gospel of audio and video developers, rapid integration of AI dubbing capability

PHP:Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocat

VirtualLab basic experiment tutorial -5 Poisson bright spot

从源码解析 MobX 响应式刷新机制

Pytest automated testing framework (II)

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

Machine learning series (5): Naive Bayes

A story on the cloud of the Centennial Olympic Games belonging to Alibaba cloud video cloud

leetcode:6094. 公司命名【分组枚举 + 不能重复用set交集 + product笛卡儿积(repeat表示长度)】
随机推荐
机器学习在美团配送系统的实践:用技术还原真实世界-笔记
Hash hash
在思科模拟器Cisco Packet Tracer实现自反ACL
CEPH deploy offline deployment of CEPH cluster and error reporting FAQ
[Huawei cloud stack] [shelf presence] issue 10: difficulties and solutions of it monitoring and diagnosis in the cloud scenario of government enterprise hybrid in the cloud native Era
OpenGL shadow implementation (soft shadow)
笔记本电脑清灰打硅脂后,开机一直黑屏,如何破?
Difference between rxjs of() and of ({})
配送交付时间轻量级预估实践-笔记
实验10 Bezier曲线生成-实验提高-控制点生成B样条曲线
标准库template学习入门原创
Still using Microsoft office, 3 fairy software, are you sure you don't want to try?
用一个性能提升了666倍的小案例说明在TiDB中正确使用索引的重要性
Shenzhen has been shut down for 7 days since March 14. Home office experience | community essay solicitation
Adjust CEPH cluster image source
2022.6.12-----leetcode.890
在思科模擬器Cisco Packet Tracer實現自反ACL
[blockbuster release] ant dynamic card, enabling the app home page to realize agile update
Partial scratch and corrosion detection of bolts and screws based on Halcon
Mysql ->>符号用法 Json相关