当前位置:网站首页>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;
}
}边栏推荐
- [Android -- data storage] use SQLite to store data
- 爬虫(17) - 面试(2) | 爬虫面试题库
- AutoLISP series (3): function function 3
- 字节跳动Android金三银四解析,android面试题app
- 第九届 蓝桥杯 决赛 交换次数
- Arduino 控制的双足机器人
- LocalStorage和SessionStorage
- Pisa-Proxy SQL 解析之 Lex & Yacc
- Sqlserver2014+: create indexes while creating tables
- Direct dry goods, 100% praise
猜你喜欢

如何快速检查钢网开口面积比是否符合 IPC7525

Opencv configuration 2019vs

Personal notes of graphics (1)

值得一看,面试考点与面试技巧
最新高频Android面试题目分享,带你一起探究Android事件分发机制

Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions

skimage学习(1)

Master this promotion path and share interview materials
![[Android -- data storage] use SQLite to store data](/img/f6/a4930276b3da25aad3ab1ae6f1cf49.png)
[Android -- data storage] use SQLite to store data

The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
随机推荐
数据中台落地实施之法
[designmode] template method pattern
1亿单身男女“在线相亲”,撑起130亿IPO
Introduction and use of gateway
网关Gateway的介绍与使用
直接上干货,100%好评
Personal notes of graphics (2)
Pisa-Proxy SQL 解析之 Lex & Yacc
Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
二叉搜索树(特性篇)
一文读懂数仓中的pg_stat
os、sys、random标准库主要功能
LocalStorage和SessionStorage
正在准备面试,分享面经
Sort out several important Android knowledge and advanced Android development interview questions
LeetCode 1043. 分隔数组以得到最大和 每日一题
Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images
QT 图片背景色像素处理法
【PHP】PHP接口继承及接口多继承原理与实现方法
字节跳动高工面试,轻松入门flutter