原题链接在这里: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 weight x is totally destroyed, and the stone of weight y has new weight y-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. 1 <= stones.length <= 30
  2. 1 <= 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;
}
}

Last Stone Weight进阶.

LeetCode 1049. Last Stone Weight II的更多相关文章

  1. leetcode 1049 Last Stone Weight II(最后一块石头的重量 II)

    有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出任意两块石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == y,那么 ...

  2. LeetCode 1046. Last Stone Weight

    原题链接在这里:https://leetcode.com/problems/last-stone-weight/ 题目: We have a collection of rocks, each roc ...

  3. Leetcode--Last Stone Weight II

    Last Stone Weight II 欢迎关注H寻梦人公众号 You are given an array of integers stones where stones[i] is the we ...

  4. 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 ...

  5. 动态规划-Last Stone Weight II

    2020-01-11 17:47:59 问题描述: 问题求解: 本题和另一题target sum非常类似.target sum的要求是在一个数组中随机添加正负号,使得最终得到的结果是target,这个 ...

  6. leetcode_1049. Last Stone Weight II_[DP]

    1049. Last Stone Weight II https://leetcode.com/problems/last-stone-weight-ii/ 题意:从一堆石头里任选两个石头s1,s2, ...

  7. LeetCode 1046. 最后一块石头的重量(1046. Last Stone Weight) 50

    1046. 最后一块石头的重量 1046. Last Stone Weight 题目描述 每日一算法2019/6/22Day 50LeetCode1046. Last Stone Weight Jav ...

  8. [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. ...

  9. [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. ...

随机推荐

  1. c++11多线程记录5: Unique Lock和延时初始化

    https://www.youtube.com/user/BoQianTheProgrammer 视频网址 Unique Lock unique_lock和lock_guard类似,都是mutex的w ...

  2. Django-07-Model操作

    一.数据库的配置 1. 数据库支持 django默认支持sqlite.mysql.oracle.postgresql数据库  <1> sqlite django默认使用sqlite的数据库 ...

  3. 怎样使用U盘安装系统

    准备工作 一个8G及以上的U盘: 软碟通UltraISO,下载地址,非免费,但试用就够了: 系统镜像,推荐去MSDN下载: 安装过程 利用U盘制作启动盘,准备好上述的东西,然后开始制作启动盘: 注意: ...

  4. Maven 初学+http://mvnrepository.com/

    了解 maven是一款服务于java平台的自动化构建工具(项目管理工具) 构建:全方位.多角度.深层次地建立 项目构建是一个项目从:源代码.编译.测试.打包.部署.运行的过程 用来解决团队开发遇到的问 ...

  5. Bootstrap中的datetimepicker用法总结

    bootstrap时间控件参考文档(少走弯路) https://blog.csdn.net/hizzyzzh/article/details/51212867#31-format-%E6%A0%BC% ...

  6. C#实现RSA加密解密

    RSA介绍 RSA公钥加密算法是1977年由Ron Rivest、Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的。RSA取名来自开发他们三者的名字。 RSA的缺点: 产生密钥 ...

  7. 自学Python编程的第\七天----------来自苦逼的转行人

    2019-09-17-23:09:48 今天学的内容是有关小数据池的,学的有点懵逼,感觉越来越难学了,但是得坚持下去 明天学习下一个课程时,感觉要跟不上,看来明天得先看好几遍今天的内容 不然肯定会听的 ...

  8. centos7安装face_recognition踩各种坑

    要在阿里云服务器上部署face_recognition.用的是centos7.  千辛万苦啊.感谢网上的各种解答.回报社会,我也把各种坑写下了.整理的有点乱.不过仔细看,有干货的. 感谢这个博主Fat ...

  9. Java自学-控制流程 continue

    Java的 continue语句 继续下一次循环 示例 1 : continue 如果是双数,后面的代码不执行,直接进行下一次循环 public class HelloWorld { public s ...

  10. yum -y install java-1.8.0-openjdk-devel.x86_64

    yum -y install java-1.8.0-openjdk-devel.x86_64