当前位置:网站首页>LeetCode 1049. Weight of the last stone II daily question
LeetCode 1049. Weight of the last stone II daily question
2022-07-07 16:58:00 【@Little safflower】
Problem description
There is a pile of stones , Use an array of integers stones Express . among stones[i] It means the first one i The weight of a stone .
Every round , Choose any two stones , Then smash them together . Suppose the weights of the stones are x and y, And x <= y. The possible results of crushing are as follows :
If x == y, Then both stones will be completely crushed ;
If x != y, So the weight is x The stone will be completely crushed , And the weight is y The new weight of the stone is y-x.
Last , There's only one piece left at most stone . Return to this stone The smallest possible weight . If there is no stone left , Just go back to 0.Example 1:
Input :stones = [2,7,4,1,8,1]
Output :1
explain :
Combine 2 and 4, obtain 2, So the array turns into [2,7,1,8,1],
Combine 7 and 8, obtain 1, So the array turns into [2,1,1,1],
Combine 2 and 1, obtain 1, So the array turns into [1,1,1],
Combine 1 and 1, obtain 0, So the array turns into [1], This is the optimal value .
Example 2:Input :stones = [31,26,33,21,40]
Output :5
Tips :
1 <= stones.length <= 30
1 <= stones[i] <= 100source : Power button (LeetCode)
link :https://leetcode.cn/problems/last-stone-weight-ii
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Java
class Solution {
public int lastStoneWeightII(int[] stones) {
int n = stones.length;
int sum = 0;
for(int i : stones){
sum += i;
}
int[] dp = new int[sum / 2 + 1];
for(int i = 0;i < n;i++){
for(int j = sum / 2;j >= stones[i];j--){
dp[j] = Math.max(dp[j],dp[j - stones[i]] + stones[i]);
}
}
return sum - dp[sum / 2] * 2;
}
}边栏推荐
- QML beginner
- 作为Android开发程序员,android高级面试
- 蓝桥杯 决赛 异或变换 100分
- QML初学
- 应用在温度检测仪中的温度传感芯片
- "The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
- [medical segmentation] attention Unet
- dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
- Master this promotion path and share interview materials
- JS中null NaN undefined这三个值有什么区别
猜你喜欢
随机推荐
【Seaborn】组合图表:PairPlot和JointPlot
掌握这套精编Android高级面试题解析,oppoAndroid面试题
《产品经理必读:五种经典的创新思维模型》的读后感
二叉搜索树(特性篇)
[designmode] facade patterns
DNS 系列(一):为什么更新了 DNS 记录不生效?
Personal notes of graphics (2)
全网“追杀”钟薛高
Module VI
Pycharm terminal enables virtual environment
整理几个重要的Android知识,高级Android开发面试题
1亿单身男女“在线相亲”,撑起130亿IPO
Master this promotion path and share interview materials
Direct dry goods, 100% praise
低代码(lowcode)帮助运输公司增强供应链管理的4种方式
Record the migration process of a project
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
【DesignMode】模板方法模式(Template method pattern)
skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值
Prometheus API deletes all data of a specified job







![[C language] question set of X](/img/17/bfa57de183c44cf0a3c6637bb65a9d.jpg)
