当前位置:网站首页>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;
}
}
边栏推荐
- Follow teacher Tian to learn practical English Grammar (continuous update)
- 重磅 | 开放原子校源行活动正式启动
- 使用R包skimr汇总统计量的优美展示
- Less than 10% of the 3 software test interview questions can be answered correctly! How many do you know?
- 主子仓库都修改,如何进行同步?
- Easy to understand and explain the gradient descent method!
- What is the difference between a global index and a local index?
- Is error log monitoring enough? Don't try JVM monitoring of microservices
- What happens when MySQL tables change from compressed tables to ordinary tables
- [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
猜你喜欢

QT基本工程的解析

为什么要使用markdown进行写作?

Conference OA project - my approval

WPF 截图控件之绘制方框与椭圆(四) 「仿微信」

Meeting OA project (V) -- meeting notice and feedback details

重磅 | 基金会为白金、黄金、白银捐赠人授牌

2018-UperNet ECCV
![[paper reading] i-bert: integer only Bert quantification](/img/2e/4f574b266ec6fc88ffa5dab56f2b8d.png)
[paper reading] i-bert: integer only Bert quantification

Follow teacher Wu to learn advanced numbers - function, limit and continuity (continuous update)

数据可视化设计指南(信息图表篇)
随机推荐
ADDS:使用 PowerShell 创建 OU 结构
浅谈安科瑞灭弧式智慧用电在养老机构的应用
Atomic operation of day4 practice in 2022cuda summer training camp
VMware: use commands to update or upgrade VMware esxi hosts
[unity, C #] character keyboard input steering and rotation
[untitled]
Tell you from my accident: Mastering asynchrony is key
[IVI] 17.1 debugging pit FAQ (compilation)
Drunken driving alarm system based on stm32
Error: Protobuf syntax version should be first thing in file
Drawing box and ellipse of WPF screenshot control (IV) "imitating wechat"
1. (map tools) detailed tutorial of acrgis desktop10.5 software installation
[reading notes] the way of enterprise IT architecture transformation Alibaba's China Taiwan strategic thinking and Architecture Practice
Less than 10% of the 3 software test interview questions can be answered correctly! How many do you know?
After E-sports enters Asia, will Tencent be the next "NBA game catcher"?
StarRocks 技术内幕:实时更新与极速查询如何兼得
Adcode city code in Gaode map API
WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
ggdag 绘制DAG和因果图
R package pedquant realizes stock download and financial quantitative analysis