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. 为什么用思科里面的设备第一次ping的时候总会丢一个包呢?

    大家搞计算机的不用讲,肯定都玩过网络吧?   比如一些思科,华为,华三这些模拟器,你们总会当你第一次用某个设备去ping某个设备的时候第一包总会被丢弃.  但我相信很多人都不知道为啥 会丢弃. 今天小 ...

  2. PHP文件操作功能函数大全

    PHP文件操作功能函数大全 <?php /* 转换字节大小 */ function transByte($size){ $arr=array("B","KB&quo ...

  3. Lightoj1122 【数位DP】

    题意: 给你m个数,让你在里面挑n个组合,保证位数相差不超过2,求能够组合多少种情况: 思路: dp[i][j]代表第i个结尾为j的方案数. #include<bits/stdc++.h> ...

  4. 51nod 1449 砝码称重【天平/进制】

    题意: 给你w,n,问你在w^0,w^1,w^2...各种一个,问你能不能用这些砝码和重量为m的东西放在天平上使得天平平衡: 思路: 这个很容易联想到进制: 如果把m放在是一边的话,其实对于砝码就是纯 ...

  5. web前端篇:JavaScript基础篇(易懂小白上手快)-2

    目录 一.内容回顾: ECMAScript基础语法 1.基本数据类型和引用数据类型 2.条件判断和循环 3.赋值运算符,逻辑运算符 4.字符串的常用方法 5.数组的常用方法 6.对象 7.函数 8.日 ...

  6. Mysql 主从(转)

    转自 http://blog.csdn.net/hguisu/article/details/7325124

  7. Cordova 系列之创建一个iOS项目

    1.打开终端 2.输入命令 $ cd Desktop (PS:Desktop表示放在桌面,你可以选择放任意位置) 3.$  cordova create HelloWorld com.example. ...

  8. 黑马函数式接口学习 Stream流 函数式接口 Lambda表达式 方法引用

  9. DBUtils学习一 增删该查

    package com.mozq.jdbc.test; import java.sql.SQLException; import java.util.List; import java.util.Ma ...

  10. hdu 6319 Problem A. Ascending Rating (2018 Multi-University Training Contest 3)

    #include <stdio.h> #include <iostream> #include <cstdlib> #include <cmath> # ...