原题链接在这里: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. 手撕面试官系列(八):分布式通讯ActiveMQ+RabbitMQ+Kafka面试专题

    ActiveMQ专题 (面试题+答案领取方式见主页) 什么是 ActiveMQ? ActiveMQ 服务器宕机怎么办? 丢消息怎么办? 持久化消息非常慢. 消息的不均匀消费. 死信队列. Active ...

  2. HLP帮助文件源文件RTF文件的编写

    https://www.cnblogs.com/gaodu2003/archive/2008/12/17/1356861.html 举例说明如下: 每一节的标题在RTF文件中一般以特有的脚注($)指定 ...

  3. arm-linux-系列工具,ld,ar,as,objcopy

    ref :http://www.360doc.com/content/14/0509/09/17268421_376009916.shtml 一.编译器相关知识学习 GNU GCC简介: GNU GC ...

  4. 【写法】为什么if判断中,值要倒着写

    =============================================== 2019/8/27_第1次修改                       ccb_warlock == ...

  5. java之mybatis之一对多关联映射

    1.在一对多的关联映射中,表结构如下 2.实体类结构 User.java public class User implements Serializable{ private int id; priv ...

  6. easy ui 弹框叠加问题

    1.框架用的是.net MVC,Index页面如下所示: @{ Layout = "~/Views/Shared/_CustomerLayout.cshtml"; ViewBag. ...

  7. SpringbBoot之JPA批量更新

    菜鸟学习,不对之处,还请纠正. 需要批量更新数据库的某些数据,项目使用的是JPA,刚对mybatis熟悉一点,又换成了JPA... 有点懵. 查询了一番之后,发现可以使用 In findByIdIn( ...

  8. dump net core lldb 分析

    原文https://www.cnblogs.com/calvinK/p/9274239.html centos7 lldb 调试netcore应用的内存泄漏和死循环示例(dump文件调试) 写个dem ...

  9. Redis笔记01——win10 64位系统安装Redis 3.2.100

    前言 由于项目中需要用到Redis,所以先在自己的win10上安装来体验一下. 安装步骤 一.下载地址 Redis下载地址 我选择的是3.2.100 的 64位 zip版本 二.安装位置以及文件简介 ...

  10. 英伟达 cuda 开发套件下载

    下载地址 https://developer.nvidia.com/cuda-toolkit 安装比较简单,就不多说了.