当前位置:网站首页>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;
}
}边栏推荐
- SqlServer2014+: 创建表的同时创建索引
- skimage学习(1)
- Cesium (4): the reason why gltf model is very dark after loading
- three. JS create cool snow effect
- Prometheus API deletes all data of a specified job
- 最新阿里P7技术体系,妈妈再也不用担心我找工作了
- 字节跳动Android金三银四解析,android面试题app
- skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色
- time标准库
- LeetCode 1049. 最后一块石头的重量 II 每日一题
猜你喜欢

Cesium(3):ThirdParty/zip. js

Three. JS series (1): API structure diagram-1

Master this promotion path and share interview materials
ByteDance Android gold, silver and four analysis, Android interview question app

【MySql进阶】索引详解(一):索引数据页结构

最新2022年Android大厂面试经验,安卓View+Handler+Binder

爬虫(17) - 面试(2) | 爬虫面试题库
As an Android Developer programmer, Android advanced interview

浅浅理解.net core的路由

DNS 系列(一):为什么更新了 DNS 记录不生效?
随机推荐
LeetCode-SQL第一天
预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
LeetCode 312. 戳气球 每日一题
LeetCode 1654. 到家的最少跳跃次数 每日一题
[designmode] facade patterns
Interface oriented programming
AutoLISP series (1): function function 1
ATM系统
dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
Temperature sensor chip used in temperature detector
Pycharm terminal enables virtual environment
射线与OBB相交检测
Record the migration process of a project
time标准库
LeetCode 152. 乘积最大子数组 每日一题
Personal notes of graphics (2)
如何选择合适的自动化测试工具?
【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
Set the route and optimize the URL in thinkphp3.2.3