LeetCode 1049. Last Stone Weight II
原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/
题目:
We have a collection of rocks, each rock has a positive integer weight.
Each turn, we choose any two rocks and smash them together. Suppose the stones have weights x and y with x <= y. The result of this smash is:
- If
x == y, both stones are totally destroyed; - If
x != y, the stone of weightxis totally destroyed, and the stone of weightyhas new weighty-x.
At the end, there is at most 1 stone left. Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)
Example 1:
Input: [2,7,4,1,8,1]
Output: 1
Explanation:
We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then,
we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then,
we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.
Note:
1 <= stones.length <= 301 <= stones[i] <= 100
题解:
If we choose any two rocks, we could divide the rocks into 2 groups.
And calculate the minimum diff between 2 groups' total weight.
Since stones.length <= 30, stone weight <= 100, maximum total weight could be 3000.
Let dp[i] denotes whether or not smaller group weight could be i. smaller goupe total weight is limited to 1500. Thus dp size is 1501. It becomes knapsack problem.
dp[0] = true. We don't need to choose any stone, and we could get group weight as 0.
For each stone, if we choose it we track dp[i-w] in the previoius iteration. If we don't choose it, track dp[i] from last iteration. Thus it is iterating from big to small.
Time Complexity: O(n*sum). n = stones.length. sum is total weight of stones.
Space: O(sum).
AC Java:
class Solution {
public int lastStoneWeightII(int[] stones) {
if(stones == null || stones.length == 0){
return 0;
}
int sum = 0;
boolean [] dp = new boolean[1501];
dp[0] = true;
for(int w : stones){
sum += w;
for(int i = Math.min(sum, 1501); i>=w; i--){
dp[i] = dp[i] | dp[i-w];
}
}
for(int i = sum/2; i>=0; i--){
if(dp[i]){
return sum-i-i;
}
}
return 0;
}
}
LeetCode 1049. Last Stone Weight II的更多相关文章
- leetcode 1049 Last Stone Weight II(最后一块石头的重量 II)
有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出任意两块石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == y,那么 ...
- LeetCode 1046. Last Stone Weight
原题链接在这里:https://leetcode.com/problems/last-stone-weight/ 题目: We have a collection of rocks, each roc ...
- Leetcode--Last Stone Weight II
Last Stone Weight II 欢迎关注H寻梦人公众号 You are given an array of integers stones where stones[i] is the we ...
- leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval
lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...
- 动态规划-Last Stone Weight II
2020-01-11 17:47:59 问题描述: 问题求解: 本题和另一题target sum非常类似.target sum的要求是在一个数组中随机添加正负号,使得最终得到的结果是target,这个 ...
- leetcode_1049. Last Stone Weight II_[DP]
1049. Last Stone Weight II https://leetcode.com/problems/last-stone-weight-ii/ 题意:从一堆石头里任选两个石头s1,s2, ...
- LeetCode 1046. 最后一块石头的重量(1046. Last Stone Weight) 50
1046. 最后一块石头的重量 1046. Last Stone Weight 题目描述 每日一算法2019/6/22Day 50LeetCode1046. Last Stone Weight Jav ...
- [leetcode]364. Nested List Weight Sum II嵌套列表加权和II
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
随机推荐
- 【rt-thread】2、尝试用ENV添加18b20传感器
尝试用ENV添加18b20传感器 rt-thread能通过env工具添加或者裁剪工程,这里调试的是通过ENV添加18b20传感器. 具体程序实现,可以参考以下资料 https://www.rt-thr ...
- 阿里巴巴 Java 开发手册 (十)MySQL 数据库
(一) 建表规约 1. [强制]表达是与否概念的字段,必须使用 is_xxx 的方式命名,数据类型是 unsigned tinyint ( 1 表示是,0 表示否). 说明:任何字段如果为非负数,必须 ...
- 用HTML、CSS、JS制作圆形进度条(无动画效果)
逻辑 1.首先有一个圆:蓝色的纯净的圆,效果: 2.再来两个半圆,左边一个,右边一个将此蓝色的圆盖住,效果: 此时将右半圆旋转60°,就会漏出底圆,效果: 然后我们再用一个比底圆小的圆去覆盖这个大 ...
- 图像平移 cv.warpAffine()函数用法
# 图像平移image1='C:\\Users\\10107472\\Desktop\\myfile\\tensorflow-yolov\\read.jpg'img = cv.imread(image ...
- mybatis 变更xml文件目录
mybatis的xml默认读取的是resources目录,这个目录是可以变化的.我习惯于将mapper文件和xml放到一起或相邻目录下. 如图: 具体操作: 以mybatis-plus为例 boots ...
- Android 下拉列表Spinner 使用
1.布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- 如何使用Prometheus采集SAP ABAP Netweaver的应用日志数据
Prometheus是一套开源的系统监控报警框架.它启发于Google的borgmon 监控系统,由工作在 SoundCloud 的 google 前员工在 2012 年创建,作为社区开源项目进行开发 ...
- linux技能五 文件权限
文件权限:-rw-r--r--. 1 fileInUser fileInGroup 1623 5月 4 19:33 fileName -:第一个-是文件类型 rw-:文件的所有者权限 r--:文件的 ...
- PHP实现财务审核通过后返现金额到客户
应用场景: 有这么一个返现的系统,当前端客户发起提现的时候,后端就要通过审核这笔返现订单,才可以返现到客户的账号里. 来看看下面的截图 这里的业务场景就是经过两轮审核:销售审核,财务审核都通过后,后端 ...
- jmeter的简单使用0723
一.添加http请求 1.右击线程组---添加---取样器---http请求,具体内容如下图所示.如果请求带参数,则要点击下方的添加按钮来添加参数 2.查看请求结果,同样右击线程组-添加---监听器- ...