1049. Last Stone Weight II

https://leetcode.com/problems/last-stone-weight-ii/

题意:从一堆石头里任选两个石头s1,s2,若s1==s2,则两个石头都被销毁,否则加入s1<s2,剩下一块重量为s2-s1的石头。重复上面的操作,直至只剩一块石头,或没有石头。问最后剩下的石头的重量最小为多少(0表示没有剩余石头)。

解法:

每次选两块石头进行相减问最后一块石头重量最小为多少,可以看作是将所有石头分为两波,使两波石头的差值的绝对值最小。

使用背包(snapsack)解决。dp[i]表示stones是否能组成重量为i的group。

class Solution
{
public:
int lastStoneWeightII(vector<int>& stones)
{
int sum = accumulate(stones.begin(),stones.end(),);
vector<bool> dp(sum/,);
dp[]=;
for(int s:stones)
for(int j=sum/;j>=s;j--)
dp[j] = dp[j]|dp[j-s];
int res = sum;
for(int i=;i<=sum/;i++)
res = min(res, sum-dp[i]*i*);
return res;
}
};

leetcode_1049. Last Stone Weight II_[DP]的更多相关文章

  1. LeetCode 1049. Last Stone Weight II

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

  2. Leetcode--Last Stone Weight II

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

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

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

  4. LeetCode 1046. Last Stone Weight

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

  5. 【Leetcode_easy】1046. Last Stone Weight

    problem 1046. Last Stone Weight 参考 1. Leetcode_easy_1046. Last Stone Weight; 完

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

  7. zoj 3706 Break Standard Weight(dp)

    Break Standard Weight Time Limit: 2 Seconds                                     Memory Limit: 65536 ...

  8. HDU 4248 A Famous Stone Collector 组合数学dp ****

    A Famous Stone Collector Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  9. 【leetcode】1046. Last Stone Weight

    题目如下: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose t ...

随机推荐

  1. python之log日志模块

    logging的配置大致有下面几种方式. 1.        通过代码进行完整配置,logging.getLogger()获取logger后,给logger设置各种handler. 2.       ...

  2. E20180608-hm

    更新: 2019/02/19 原来忘记分类,把此博文归入单词类 capacity  n. 容量; 性能; 才能; 生产能力;

  3. C++继承详解:共有(public)继承,私有(private)继承,保护(protected)继承

    公有继承(public).私有继承(private).保护继承(protected)是常用的三种继承方式. 1. 公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时, ...

  4. Unity json

    MiniJSON.cs using UnityEngine; using System; using System.Collections; using System.Collections.Gene ...

  5. unity sprite怎么获取切割后的图

    学习了一段时间的unity,对里面的组件有一个大致的了解,但是具体操作来说还不是很熟悉,今天看了一片关于unity sprite怎么获取切割后的图的文章,感觉还不错. 假设有一张png/tga图集,导 ...

  6. Linux 一些问题

    终端以root账号执行 su - root

  7. hoj2798 Globulous Gumdrops

    Globulous Gumdrops My Tags   (Edit)   Source : 2008 Stanford Programming Contest   Time limit : 1 se ...

  8. 洛谷P2824 [HEOI2016/TJOI2016]排序(线段树)

    传送门 这题的思路好清奇 因为只有一次查询,我们考虑二分这个值为多少 将原序列转化为一个$01$序列,如果原序列上的值大于$mid$则为$1$否则为$0$ 那么排序就可以用线段树优化,设该区间内$1$ ...

  9. windows 修改鼠标滚轮自然滚动

    在mac 上玩习惯了,使用windows 时的鼠标实在觉得别扭,在网上百度了一下,找到一个方法,这里记录一下 1 打开windows 的控制面板,点击“硬件和声音” 2 点击“鼠标” 3 然后点击上面 ...

  10. Luogu P4892 GodFly的寻宝之旅【状压dp】By cellur925

    题目传送门 又是一道状压+计数类好题hh(真香).数据范围非常友好,告诉我们\(n<=18\),非常符合状压的性质. 其实感觉和\(Hamilton\)路径那题还是有些相似的,我们可以类似地设计 ...