当前位置:网站首页>LeetCode_ 1049_ Weight of the last stone II
LeetCode_ 1049_ Weight of the last stone II
2022-07-29 10:49:00 【Fitz1318】
Topic link
Title 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 from 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 <= 301 <= stones[i] <= 100
Their thinking
Dynamic programming
Try to divide the stones into two equal piles , The stones left after the collision are the smallest , Therefore, it is converted into 01 knapsack problem
Dynamic programming five steps
- determine
dpThe meaning of array and subscriptdp[j]: Capacity ofjThe backpack , You can recite at mostdp[j]Heavy stone
- Determine the recurrence formula
- stay 01 In the backpack , The recurrence formula is
dp[j] = Math.max(dp[j],dp[j-weight[i] + value[i]]),dp[j] = Math.max(dp[j],dp[j-stones[i]] + stones[i])
- stay 01 In the backpack , The recurrence formula is
dpArray initializationdp[0] = 0, Other subscriptsdp[i] = 0
- Determine the traversal order
- Traversal of items for Loop on the outer layer , Traversing the backpack for Loop on the inner layer , And inner for Loop backward traversal
- For example, push to
dpArray- Finally, it is divided into two piles of stones , The total weight of a pile is
dp[bagWeight], The other pile issum - dp[bagWeight] - So finally back to
sum - dp[bagWeight] * 2
- Finally, it is divided into two piles of stones , The total weight of a pile is
AC Code
class Solution {
public int lastStoneWeightII(int[] stones) {
int sum = 0;
for (int i : stones) {
sum += i;
}
int bagWeight = sum / 2;
int[] dp = new int[bagWeight + 1];
dp[0] = 0;
for (int i = 0; i < stones.length; i++) {
for (int j = bagWeight; j >= stones[i]; j--) {
dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);
}
}
return sum - dp[bagWeight] * 2;
}
}
边栏推荐
- Static resource mapping
- Spark高效数据分析01、idea开发环境搭建
- MySQL 8 of relational database -- deepening and comprehensive learning from the inside out
- How to realize the function of adding watermark
- Ggdag draw DAG and cause and effect diagram
- Summer 2022 software innovation laboratory training JDBC
- Adcode city code in Gaode map API
- Basic construction of QT project
- Review of the 16th issue of HMS core discovery | play with the new "sound" state of AI with tiger pier
- The heavy | open atomic school source activity was officially launched
猜你喜欢

数据可视化设计指南(信息图表篇)

If distributed file storage is realized according to integrated Minio

Open source, compliance escort! 2022 open atom global open source summit open source compliance sub forum is about to open

Drunken driving alarm system based on stm32

Site data collection -scrapy usage notes

Summer 2022 software innovation laboratory training JDBC

Second handshake?? Three waves??

ES6-箭头函数this指向

若依如何实现添加水印功能

Object storage
随机推荐
Oracle advanced (XIV) explanation of escape characters
Docker installation, redis configuration and remote connection
The server
Basic construction of QT project
Static resource mapping
R language Monte Carlo method and average method are used to calculate the definite integral. Considering the random point casting method, the confidence is 0.05, and the requirement is ϵ= 0.01, numbe
基于STM32设计的酒驾报警系统
What are the compensation standards for hospital misdiagnosis? How much can the hospital pay?
mosquitto_ Sub -f parameter use
After E-sports enters Asia, will Tencent be the next "NBA game catcher"?
Spark高效数据分析02、基础知识13篇
Data office system
Performance optimization analysis tool | perf
[untitled]
2022cuda summer training camp Day2 practice
Kunlunbase support for MySQL private DML syntax
一文搞懂什么是二叉树(二叉树的种类、遍历方式、定义)
使用 RTCGA 临床数据进行生存分析
可线性渐变的环形进度条的实现探究
ADB shell WM command and usage: