当前位置:网站首页>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;
}
}
边栏推荐
- 两类更新丢失及解决办法
- 【Seaborn】组合图表:PairPlot和JointPlot
- logback. XML configure logs of different levels and set color output
- [PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
- 射线与OBB相交检测
- LeetCode 403. 青蛙过河 每日一题
- OpenGL personal notes
- Introduction and use of gateway
- LeetCode 1186. 删除一次得到子数组最大和 每日一题
- os、sys、random标准库主要功能
猜你喜欢
记录Servlet学习时的一次乱码
字节跳动高工面试,轻松入门flutter
【DesignMode】外观模式 (facade patterns)
[C language] question set of X
作为Android开发程序员,android高级面试
Temperature sensor chip used in temperature detector
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid
Three. JS series (1): API structure diagram-1
Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
随机推荐
字节跳动Android面试,知识点总结+面试题解析
URL和URI的关系
面试题 01.02. 判定是否互为字符重排-辅助数组算法
[designmode] template method pattern
整理几个重要的Android知识,高级Android开发面试题
LeetCode 403. 青蛙过河 每日一题
【DesignMode】外观模式 (facade patterns)
os、sys、random标准库主要功能
A tour of gRPC:03 - proto序列化/反序列化
Opencv personal notes
C语言进阶——函数指针
Introduction to ThinkPHP URL routing
Module VI
Usage of config in laravel
【图像传感器】相关双采样CDS
LeetCode 1774. 最接近目标价格的甜点成本 每日一题
Geoserver2.18 series (5): connect to SQLSERVER database
编程模式-表驱动编程
SqlServer2014+: 创建表的同时创建索引
LeetCode 1986. 完成任务的最少工作时间段 每日一题