LeetCode 1049. Last Stone Weight II
原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/
题目:
We have a collection of rocks, each rock has a positive integer weight.
Each turn, we choose any two rocks and smash them together. Suppose the stones have weights x
and y
with x <= y
. The result of this smash is:
- If
x == y
, both stones are totally destroyed; - If
x != y
, the stone of weightx
is totally destroyed, and the stone of weighty
has new weighty-x
.
At the end, there is at most 1 stone left. Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)
Example 1:
Input: [2,7,4,1,8,1]
Output: 1
Explanation:
We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then,
we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then,
we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.
Note:
1 <= stones.length <= 30
1 <= stones[i] <= 100
题解:
If we choose any two rocks, we could divide the rocks into 2 groups.
And calculate the minimum diff between 2 groups' total weight.
Since stones.length <= 30, stone weight <= 100, maximum total weight could be 3000.
Let dp[i] denotes whether or not smaller group weight could be i. smaller goupe total weight is limited to 1500. Thus dp size is 1501. It becomes knapsack problem.
dp[0] = true. We don't need to choose any stone, and we could get group weight as 0.
For each stone, if we choose it we track dp[i-w] in the previoius iteration. If we don't choose it, track dp[i] from last iteration. Thus it is iterating from big to small.
Time Complexity: O(n*sum). n = stones.length. sum is total weight of stones.
Space: O(sum).
AC Java:
class Solution {
public int lastStoneWeightII(int[] stones) {
if(stones == null || stones.length == 0){
return 0;
} int sum = 0;
boolean [] dp = new boolean[1501];
dp[0] = true; for(int w : stones){
sum += w; for(int i = Math.min(sum, 1501); i>=w; i--){
dp[i] = dp[i] | dp[i-w];
}
} for(int i = sum/2; i>=0; i--){
if(dp[i]){
return sum-i-i;
}
} return 0;
}
}
LeetCode 1049. Last Stone Weight II的更多相关文章
- leetcode 1049 Last Stone Weight II(最后一块石头的重量 II)
有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出任意两块石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == y,那么 ...
- LeetCode 1046. Last Stone Weight
原题链接在这里:https://leetcode.com/problems/last-stone-weight/ 题目: We have a collection of rocks, each roc ...
- 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 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 ...
- 动态规划-Last Stone Weight II
2020-01-11 17:47:59 问题描述: 问题求解: 本题和另一题target sum非常类似.target sum的要求是在一个数组中随机添加正负号,使得最终得到的结果是target,这个 ...
- leetcode_1049. Last Stone Weight II_[DP]
1049. Last Stone Weight II https://leetcode.com/problems/last-stone-weight-ii/ 题意:从一堆石头里任选两个石头s1,s2, ...
- LeetCode 1046. 最后一块石头的重量(1046. Last Stone Weight) 50
1046. 最后一块石头的重量 1046. Last Stone Weight 题目描述 每日一算法2019/6/22Day 50LeetCode1046. Last Stone Weight Jav ...
- [leetcode]364. Nested List Weight Sum II嵌套列表加权和II
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
随机推荐
- python 之 面向对象 (异常处理)
7.15 异常处理 1.什么是异常 异常是错误发生的信号,程序一旦出错,如果程序中还没有相应的处理机制,那么该错误就会产生一个异常抛出来,程序的运行也随之终止 2.一个异常分为三部分: 异常的追踪信息 ...
- day02——while、字符串格式化、运算符、编码初识
day02 while--关键字(死循环) 格式:while 条件: 循环体 print(1) while True: print("痒") print("鸡你太美& ...
- 【LEETCODE】72、分割回文串 III 第1278题
package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...
- t100 常用公用變數
g_enterprise 目前的企業代碼,將限制使用者所能閱讀的資料內容g_prog 目前執行的作業編號,用於變換畫面顯示資料與產生系統資訊,不可變更g_code 目前執行的程式代碼(4gl)名稱,不 ...
- 【题解】Luogu P5279 [ZJOI2019]麻将
原题传送门 希望这题不会让你对麻将的热爱消失殆尽 我们珂以统计每种牌出现的次数,不需要统计是第几张牌 判一副牌能不能和,类似这道题 对于这题: 设\(f[i][j][k][0/1]\)表示前\(i\) ...
- python 跨目录访问文件
1.同级.同目录的文件之间的访问 有这样一个目录结构 假如,in_A.py 这个文件想调用 hello_world.py 中的函数怎么办呢? --->>> import 只需在 i ...
- C++字符串相互转换
转自cs_wu原文 C++ char*,const char*,string的相互转换 1. string转const char* string s ="abc"; const c ...
- 【转载】C#中List集合使用Min()方法查找到最小值
在C#的List集合操作中,有时候需要查找到List集合中的最小值,此时可以使用List集合的扩展方法Min方法,Min方法有2种形式,一种是不带任何参数的形式,适用于一些值类型变量的List集合,另 ...
- Firebird 审计追踪
Firebird 打开审计追踪功能,即在服务器上打开日志记录功能,根据配置记录不同类型的服务器执行情况. 1.首先修改Firebird.conf文件,打开审计功能: AuditTraceConfigF ...
- 2.live555源码分析----服务端doEventLoop()函数分析
上一篇博客说道,live555服务端main函数做的最后一件事就是调用如下代码陷入死循环: env->taskScheduler().doEventLoop(); // does not ret ...