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 ...
随机推荐
- python 内置函数的补充 isinstance,issubclass, hasattr ,getattr, setattr, delattr,str,del 用法,以及元类
isinstance 是 python中的内置函数 , isinstance()用来判断一个函数是不是一个类型 issubclass 是python 中的内置函数, 用来一个类A是不是另外一个 ...
- KM算法萌新讲解篇
KM算法 首先了解问题:也就是最大权值匹配: 二分图里,边带了权值,求整幅图里匹配最大/最小的权值 因为接触匈牙利算法的时候看的是找对象系列的博文,所以也自己写一发找对象的博文吧: 算法背景: 信 ...
- 一个高效的A-star寻路算法(八方向)(
这种写法比较垃圾,表现在每次搜索一个点要遍历整个地图那么大的数组,如果地图为256*256,每次搜索都要执行65535次,如果遍历多个点就是n*65535,速度上实在是太垃圾了 简单说下思路,以后补充 ...
- printf汇总
Reference: http://www.cplusplus.com/reference/cstdio/printf/ printf("%02d:%02d:%02d.%03d", ...
- [Xcode 实际操作]九、实用进阶-(24)使用Segue(页面的跳转连接)进行页面跳转并传递参数
目录:[Swift]Xcode实际操作 本文将演示使用Segue(页面的跳转连接)进行页面跳转并传递参数. 参照结合:[Xcode10 实际操作]九.实用进阶-(23)多个Storyboard故事板中 ...
- C#主从表查询
软件的使用必然涉及到主表和子表的操作,我们先在SQLite中创建子表.比如 创建一学生信息表做主表,再创建一个学生成绩表做子表.然后我们在程序中成绩 方法来连接子表. 判断bindingsource中 ...
- rn-splash-screen 启动页 安卓
1. 进入项目npm install --save rn-splash-screen 2.react-native link rn-splash-screen 3.在项目android/app/src ...
- web项目数据存入mysql数据库中文乱码问题
刚开始怀疑是项目中编码设置问题,发现在web.xml中已经有过设置:后来dubug显示数据在传输的过程中一切正常,怀疑是数据库编码问题,然后查看mysql编码: show variables like ...
- PostgreSQL - 怎么将时间转换成秒
保留原来的毫秒值 select extract(epoch from '03:21:06.678'::time); 这个extract(epoch from )函数得到的是时间是秒单位,如果需要毫秒值 ...
- PostgreSQL - pgAdmin4远程连接数据库
前言 PostgreSQL在安装的时候自带的pgAdmin这个可视化工具,自从将PostgreSQL9升级到了10版本后,自带的pgAdmin也从3升级到了4版本.pgAdmin4的变化非常巨大,刚接 ...