leetcode_1049. Last Stone Weight II_[DP]
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]的更多相关文章
- LeetCode 1049. Last Stone Weight II
原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/ 题目: We have a collection of rocks, each ...
- 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 1046. 最后一块石头的重量(1046. Last Stone Weight) 50
1046. 最后一块石头的重量 1046. Last Stone Weight 题目描述 每日一算法2019/6/22Day 50LeetCode1046. Last Stone Weight Jav ...
- LeetCode 1046. Last Stone Weight
原题链接在这里:https://leetcode.com/problems/last-stone-weight/ 题目: We have a collection of rocks, each roc ...
- 【Leetcode_easy】1046. Last Stone Weight
problem 1046. Last Stone Weight 参考 1. Leetcode_easy_1046. Last Stone Weight; 完
- 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 ...
- zoj 3706 Break Standard Weight(dp)
Break Standard Weight Time Limit: 2 Seconds Memory Limit: 65536 ...
- HDU 4248 A Famous Stone Collector 组合数学dp ****
A Famous Stone Collector Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- 【leetcode】1046. Last Stone Weight
题目如下: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose t ...
随机推荐
- mysql分页性能
- select * from userinfo limit 20000,10 # 数据越往后越慢 - 索引表中扫: select * from userinfo where id in (sele ...
- Flutter实战视频-移动电商-19.首页_火爆专区界面布局编写
19.首页_火爆专区界面布局编写 看一下图片的效果 一个标题栏,下面是多行两列.里面可以用column布局,外面用Warp流式布局 有得小伙伴说这里可以用网格布局,网格布局的话还是有一定的效率问题.这 ...
- PHP中正则表达式学习及应用(二)
正则表达式中的“元字符” * 匹配前一个内容的0次1次或多次 例如: <?php $mode="/go*gle/"; //前一个内容指的是 * 的前一个字符 o ,在$str ...
- HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】
Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- Oracle 11g client 安装
本文所有红色字体标注的为本人计算机安装方法(Oracle 11g安装在本地 Oracle 11g client 也是安装在本地 如果情况一致 可参照本人方法安装) Oracle 11g client ...
- fitnesse(gradle构建)安装步骤
1.安装jdk.ant.gradle(参考http://www.cnblogs.com/274914765qq/p/4401525.html) 2.下载Fitnesse https://github. ...
- 制作Docker镜像的两种方式
此文已由作者朱笑天授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 一.使用docker commit命令制作docker镜像 1. pull一个centos6.6的基础镜像, ...
- E20180605-hm
arbitrary adj. 乱; 随意的,任性的,随心所欲的; 主观的,武断的; 霸道的,专制的,专横的,独断独行的; session n. 开会,会议; (法庭的) 开庭; 会期,学期; (进行某 ...
- Linux下更改mysql版本
想要更改linux的mysql版本,并不需要重装系统重新选择mysql版本,只需要删除掉原来的mysql然后在安装新的就可以啦 (谨记:一定要做好数据库备份) 详情请参照:大牛博客 over!over ...
- 萌新三分讲解+基础题ZOJ3203【三分凸性】
(温馨提示:图片外部食用更加) mid=(left+right)>>1,midmid=(mid+right)>>1; 举凸性函数的例子: 首先我们一定要明确问题:求极值,这里是 ...