当前位置:网站首页>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;
}
}
边栏推荐
- typescript ts 基础知识之类型声明
- 射线与OBB相交检测
- [summary of knowledge] summary of notes on using SVN in PHP
- skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
- C语言进阶——函数指针
- Opencv personal notes
- QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
- 最新Android面试合集,android视频提取音频
- 01tire+ chain forward star +dfs+ greedy exercise one
- Personal notes of graphics (3)
猜你喜欢
随机推荐
二叉搜索树(特性篇)
Find tags in prefab in unity editing mode
最新2022年Android大厂面试经验,安卓View+Handler+Binder
time标准库
01tire+ chain forward star +dfs+ greedy exercise one
Three. JS series (1): API structure diagram-1
QT 图片背景色像素处理法
QML beginner
Temperature sensor chip used in temperature detector
[PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
Three. JS series (3): porting shaders in shadertoy
[Android -- data storage] use SQLite to store data
LeetCode 312. 戳气球 每日一题
两类更新丢失及解决办法
skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值
Imitate the choice of enterprise wechat conference room
ATM系统
谎牛计数(春季每日一题 53)
Three. JS series (2): API structure diagram-2
谈谈 SAP 系统的权限管控和事务记录功能的实现