当前位置:网站首页>Leetcode brush question: dynamic planning 09 (weight of the last stone II)
Leetcode brush question: dynamic planning 09 (weight of the last stone II)
2022-07-28 03:39:00 【Taotao can't learn English】
1049. The weight of the last stone II
questions : secondary
There is a pile of stones , The weight of each stone is a positive integer .
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 stone left . Return the minimum possible weight of this stone . If there is no stone left , Just go back to 0.
Example :
Input :[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 .
Tips :
- 1 <= stones.length <= 30
- 1 <= stones[i] <= 1000
dp[j] by Capacity of j The backpack
If you take a stone every time stones[i] be dp[j] The remaining capacity of is Math.max(dp[j],dp[j-stones[i]]+stone[i]),
If you don't take this stone , Is the same
/**
* Divide the total weight of the stones into two piles , Subtract the last , Then we can get whether there are any remaining stones in the end
* namely : Find the stone that can be loaded when the capacity is half , Compare with the remaining stone capacity , You can get the required remaining stone weight
* 1. dp[j]: Capacity of j The maximum number of stones a backpack can hold .j Take half of the total number of stones ,dp[j]<=j
* Because it needs to be subtracted from the weight of the remaining stones
* 2. Determine the recurrence formula stones It is the number of stones that the backpack has loaded ,dp[j-stones[i]] Ask for stones that can be filled in the remaining capacity of the backpack
* 3.dp Array initialization :dp[j] Take the maximum value every time , So take 0 Can ,dp[j] The initial value will not be overwritten , The weight of the stone must be >0
* 4. Determine the traversal order : First traverse the number of stones , Go through the backpack again ( In the past, it will lead to what has been obtained dp[i] Be reused )
*/
package com.programmercarl.dynamic;
/** * @ClassName LastStoneWeightII * @Descriotion TODO * @Author nitaotao * @Date 2022/7/27 15:55 * @Version 1.0 * https://leetcode.cn/problems/last-stone-weight-ii/ * 1049. The weight of the last stone II **/
public class LastStoneWeightII {
public int lastStoneWeightII(int[] stones) {
/** * Divide the total weight of the stones into two piles , Subtract the last , Then we can get whether there are any remaining stones in the end * namely : Find the stone that can be loaded when the capacity is half , Compare with the remaining stone capacity , You can get the required remaining stone weight * 1. dp[j]: Capacity of j The maximum number of stones a backpack can hold .j Take half of the total number of stones ,dp[j]<=j * Because it needs to be subtracted from the weight of the remaining stones * 2. Determine the recurrence formula stones It is the number of stones that the backpack has loaded ,dp[j-stones[i]] Ask for stones that can be filled in the remaining capacity of the backpack * 3.dp Array initialization :dp[j] Take the maximum value every time , So take 0 Can ,dp[j] The initial value will not be overwritten , The weight of the stone must be >0 * 4. Determine the traversal order : First traverse the number of stones , Go through the backpack again ( In the past, it will lead to what has been obtained dp[i] Be reused ) */
// Total stone weight and
int sum = 0;
for (int i = 0; i < stones.length; i++) {
sum += stones[i];
}
// / Rounding down sum/2>=target
int target = sum / 2;
//dp[j]: Capacity of j The maximum number of stones a backpack can hold .j Take half of the total number of stones ,dp[j]<=j
// j Capacity dp[j] Capacity of j The backpack
int[] dp = new int[target + 1];
// First traverse the stone
for (int i = 0; i < stones.length; i++) {
// Traversal from back to front
//j The initial value is the total capacity of the backpack
for (int j = target; stones[i] <= j; j--) {
//stones[i]<=j Stone volume <= Backpack Capacity
// discharge At present j-store[i] Capacity backpack loading stones[i]
dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);
}
}
// Subtract two stone heaps of the same size
return sum - dp[target] * 2;
}
public static void main(String[] args) {
System.out.println(new LastStoneWeightII().lastStoneWeightII(new int[]{
2, 7, 4, 1, 8, 1}));
}
}

Code Capriccio recording brush question stopped ,xmd, I can't brush , I'm going to listen to the class and practice 55555555555
边栏推荐
- 20220727使用汇承科技的蓝牙模块HC-05配对手机进行蓝牙串口的演示
- deepstream 检测结果截图
- 动态内存管理中的malloc、free、calloc、realloc动态内存开辟函数
- How to solve the problem of win11 black desktop background?
- In December, the PMP Exam adopted the new syllabus for the first time. How to learn?
- 动态规划——474. 一和零
- Asemi rectifier bridge gbpc5010, gbpc5010 parameters, gbpc5010 size
- 服务器内存故障预测居然可以这样做!
- 【OPENVX】对象基本使用之vx_pyramid
- 20220726 at command test of Bluetooth module hc-05 of Huicheng Technology
猜你喜欢

沃尔沃:深入人心的“安全感” 究竟靠的是什么?

LabVIEW加载和使用树型控件项目中的定制符号

4-day excel practical training camp, 0.01 yuan special offer for only three days, 200 sets of learning kits

Mouse operation and response

ASEMI整流桥GBPC3510在直流有刷电机中的妙用

After reading MySQL database advanced practice (SQL xiaoxuzhu)

53. Maximum subarray maximum subarray sum

What are the fragments of MySQL

Billions of asset addresses are blacklisted? How to use the tether address freezing function?

Methods of SQL server backup database
随机推荐
input 上传文件并回显 FileReader并限制选择文件时的类型
TypeError: ufunc ‘bitwise_and‘ not supported for the input types, and the inputs could not be safely
Unity简单实现对话功能
Assembly method of golang Gorm query arbitrary fields
如何让外网访问内网IP(esp8266网页使用)
贪心——55. 跳跃游戏
做自动化测试,你后悔了吗?
动态规划——474. 一和零
玩一玩WolframAlpha计算知识引擎
The open source of "avoiding disease and avoiding medicine" will not go far
After reading MySQL database advanced practice (SQL xiaoxuzhu)
Play WolframAlpha computing knowledge engine
Xctf attack and defense world web master advanced area unserialize3
Msgan is used for pattern search of multiple image synthesis to generate confrontation Network -- to solve the problem of pattern collapse
Leetcode 29th day
Mouse operation and response
redis源码分析(谁说C语言就不能分析了?)
What is tor? What is the use of tor browser update?
The latest version of pagoda installs the zip extension, and PHP -m does not display the processing method
20220726 at command test of Bluetooth module hc-05 of Huicheng Technology
