当前位置:网站首页>LeetCode_1049_最后一块石头的重量Ⅱ
LeetCode_1049_最后一块石头的重量Ⅱ
2022-07-29 10:45:00 【Fitz1318】
题目链接
题目描述
有一堆石头,用整数数组 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 <= 301 <= stones[i] <= 100
解题思路
动态规划
尽量将石头分为两堆相等的石头,这样相撞之后剩下的石头最小,因此就转换成01背包问题
动态规划五部曲
- 确定
dp数组及下标的含义dp[j]:容量为j的背包,最多可以背dp[j]重的石头
- 确定递推公式
- 在01背包中,递推公式为
dp[j] = Math.max(dp[j],dp[j-weight[i] + value[i]]),dp[j] = Math.max(dp[j],dp[j-stones[i]] + stones[i])
- 在01背包中,递推公式为
dp数组初始化dp[0] = 0,其他下标dp[i] = 0
- 确定遍历顺序
- 遍历物品的for循环放在外层,遍历背包的for循环放在内层,且内层的for循环倒序遍历
- 举例推到
dp数组- 最后分成两堆石头,一堆总重量是
dp[bagWeight],另一堆是sum - dp[bagWeight] - 所以最终返回
sum - dp[bagWeight] * 2
- 最后分成两堆石头,一堆总重量是
AC代码
class Solution {
public int lastStoneWeightII(int[] stones) {
int sum = 0;
for (int i : stones) {
sum += i;
}
int bagWeight = sum / 2;
int[] dp = new int[bagWeight + 1];
dp[0] = 0;
for (int i = 0; i < stones.length; i++) {
for (int j = bagWeight; j >= stones[i]; j--) {
dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);
}
}
return sum - dp[bagWeight] * 2;
}
}
边栏推荐
- Review of the 16th issue of HMS core discovery | play with the new "sound" state of AI with tiger pier
- 【Unity,C#】Character键盘输入转向与旋转
- What happens when MySQL tables change from compressed tables to ordinary tables
- Meeting OA project (V) -- meeting notice and feedback details
- Object storage
- After E-sports enters Asia, will Tencent be the next "NBA game catcher"?
- Research on Android multithreading (4) -- from an interview question
- 浅谈安科瑞灭弧式智慧用电在养老机构的应用
- Site data collection -scrapy usage notes
- Use R-Pack skimr to collect the beautiful display of President measurement
猜你喜欢

Alibaba P8 broke out this interview guide for big factories. After reading it, the salary soared by 30K!

主子仓库都修改,如何进行同步?

Analysis of QT basic engineering

若依如何实现添加水印功能

How to realize the function of adding watermark

StarRocks 技术内幕:实时更新与极速查询如何兼得

Research on the realization of linear gradient circular progress bar

VMware: use commands to update or upgrade VMware esxi hosts

阿里架构师耗时一年整理的《Lucene高级文档》,吃透你也是大厂员工!

Comprehensive and detailed SQL learning guide (MySQL direction)
随机推荐
Drunken driving alarm system based on stm32
Roots of equations in R language dichotomy and Newton iteration
Performance optimization analysis tool | perf
R language Monte Carlo method and average method are used to calculate the definite integral. Considering the random point casting method, the confidence is 0.05, and the requirement is ϵ= 0.01, numbe
[dark horse morning post] Youxian responded to the dissolution every day, and many places have been unable to place orders; Li Bin said that Wei Lai will produce a mobile phone every year; Li Ka Shing
After E-sports enters Asia, will Tencent be the next "NBA game catcher"?
使用R包PreMSIm根据基因表达量来预测微卫星不稳定
专访 | 阿里巴巴首席技术官程立:云 + 开源共同形成数字世界的可信基础
Drawing box and ellipse of WPF screenshot control (IV) "imitating wechat"
Why use markdown to write?
MySQL 8 of relational database -- deepening and comprehensive learning from the inside out
Open source, compliance escort! 2022 open atom global open source summit open source compliance sub forum is about to open
Implementation of college logistics repair application system based on SSM
浅谈安科瑞灭弧式智慧用电在养老机构的应用
从零开始Blazor Server(3)--添加cookie授权
StarRocks 技术内幕:实时更新与极速查询如何兼得
Object storage
聊聊性能测试环境搭建
Docker installation, redis configuration and remote connection
js两个数组对象进行合并去重