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. ...
随机推荐
- golang module 下载外网资源失败解决办法
用 golang 1.11 module 特性时,需要下载golang.org等外网地址的库文件 可以创建环境变量GOPROXY,使用Aliyun的镜像 go公共代理文档 简介 go module公共 ...
- Golang修改json文件的两种方法
第三方包 go get -u github.com/tidwall/sjson bytes, _ := ioutil.ReadFile(jsonFile) value1, _ := sjson.Set ...
- 单点logi,n
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; u ...
- Java之路---Day10(抽象)
2019-10-24-23:21:17 目录 1.抽象的方法 2.抽象类 3.抽象类和抽象方法的使用 4.抽象类的注意事项 5.案例代码 1.抽象的方法 What:如果父类当中的方法不确定如何进行{} ...
- requirejs:模块加载(require)及定义(define)时的路径理解
给新来的实习生普及下JS基本知识,看到比较好的文章 转载https://blog.csdn.net/xuxiaoping1989/article/details/52384778 接触过require ...
- 一张图带你看懂原始dao与SQL动态代理开发的区别-Mybatis
//转载请注明出处:https://www.cnblogs.com/nreg/p/11156167.html 1.项目结构区别: 2.开发区别: 注:其中原始dao开发的实现类UserDaoImpl ...
- Win 10 无法锁屏,快捷键win+L失效
快捷键win+L 一直在使用,忽然之间不知道按错了什么 Win 10 无法锁屏,快捷键win+L失效,按win+L后出来的是输入法 应该是键盘的Windows键锁住了,按Fn+windows键解锁
- Shallow copy and Deep copy
Shallow copy and Deep copy 第一部分: 一.来自wikipidia的解释: Shallow copy One method of copying an object is t ...
- tcp、udp协议栈
tcp struct tcphdr { __be16 source; //源端口 __be16 dest; //目的端口 __be32 seq; //序列号 __be32 ack_seq; //确认号 ...
- 查看kafka版本
kafka没有提供version命令,不确定是否有方便的方法,但你可以进入kafka/libs文件夹. 或: find / -name \*kafka_\* | head -1 | grep -o ' ...