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 ...
随机推荐
- Java 类加载器的作用
深入探讨 Java 类加载器 成 富, 软件工程师, IBM 中国软件开发中心 成富任职于 IBM 中国软件开发中心,目前在 Lotus 部门从事 IBM Mashup Center 的开发工作.他毕 ...
- 《剑指offer》面试题13—O(1)时间删除链表结点
题目:给定单向链表的头指针和某结点指针,实现函数在O(1)时间内删除指定节点. 思路:由于没有要删除结点(j结点)的前一个结点(i结点)指针,通常想法是从头开始遍历找到指定结点的前一个结点(i结点), ...
- 使用Try.NET创建可交互.NET文档
原文地址:Create Interactive .NET Documentation with Try .NET 原文作者:Maria 译文地址:https://www.cnblogs.com/lwq ...
- sql查询的时候,等于这两个的值得全部取出来
sql查询的时候 用or连接 ad.jqtype='人文历史' or ad.jqtype='名胜古迹'
- 一个github搞定微信小程序支付系列
详情请前往github下载示例代码 源码中包含 支付.退款 功能 so easy,项目经理再也不用担心微信支付啦 是的,已经over了
- 51nod 1068【简单博弈】
思路 手动打表, N 1 : A出1 A胜: 2 : A出2 A胜: 3 : A只能出2的整数幂&&这个数<=3,所以只能出1,2:A出1的时候,B就是2的情况,B胜:A出2的时 ...
- qscoj53(图的m着色问题)
题目链接:http://qscoj.cn/contest/12/problem/53/ 题意:中文题诶- 思路:n个点, 那么最多用n种颜色,所以我们可以枚举颜色种类1~n,然后再判断用 i 种颜色可 ...
- suse 11 sp4 设置yast 安装源
suse yast 软件管理工具,用户在初始安装系统时,可能会遗漏比较多的库和工具,那么为了方便大家日后可以随时添加,用户可以选择将安装ISO 文件添加到 suse 的yast 安装源上. 用户首先创 ...
- javascript 中not defined 和undefined有什么区别
概念上的解释:undefined是javascript语言中定义的五个原始类中的一个,换句话说,undefined并不是程序报错,而是程序允许的一个值.not defined是javascript在运 ...
- NOIP前模拟赛总结
NOIP前模拟赛总结 from 2018.10.7 to ??? Date Name Score(Rank) Problems 2018.10.7 McfXH AK Contest 42(?) 期望得 ...