[Swift]LeetCode1049.最后一块石头的重量 II | Last Stone Weight II
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10885064.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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
有一堆石头,每块石头的重量都是正整数。
每一回合,从中选出任意两块石头,然后将它们一起粉碎。假设石头的重量分别为 x
和 y
,且 x <= y
。那么粉碎的可能结果如下:
- 如果
x == y
,那么两块石头都会被完全粉碎; - 如果
x != y
,那么重量为x
的石头将会完全粉碎,而重量为y
的石头新重量为y-x
。
最后,最多只会剩下一块石头。返回此石头最小的可能重量。如果没有石头剩下,就返回 0
。
示例:
输入:[2,7,4,1,8,1] 输出:1 解释: 组合 2 和 4,得到 2,所以数组转化为 [2,7,1,8,1], 组合 7 和 8,得到 1,所以数组转化为 [2,1,1,1], 组合 2 和 1,得到 1,所以数组转化为 [1,1,1], 组合 1 和 1,得到 0,所以数组转化为 [1],这就是最优值。
提示:
1 <= stones.length <= 30
1 <= stones[i] <= 1000
1 class Solution { 2 let MAX:Int = 3005 3 func lastStoneWeightII(_ stones: [Int]) -> Int { 4 var possible:[Bool] = [Bool](repeating:false,count:2 * MAX + 1) 5 possible[MAX] = true 6 for stone in stones 7 { 8 var next_possible:[Bool] = [Bool](repeating:false,count:2 * MAX + 1) 9 for x in 0...2 * MAX 10 { 11 if possible[x] 12 { 13 14 next_possible[x + stone] = true 15 next_possible[x - stone] = true 16 } 17 } 18 possible = next_possible 19 } 20 for i in 0...MAX 21 { 22 if possible[MAX + i] 23 { 24 return i 25 } 26 } 27 return -1 28 } 29 }
转载于:https://www.cnblogs.com/strengthen/p/10885064.html
[Swift]LeetCode1049.最后一块石头的重量 II | Last Stone Weight II相关推荐
- leetcode1049. 最后一块石头的重量 II(java)
最后一块石头的重量 II leetcode1049. 最后一块石头的重量 II 题目描述 解题思路 解法一 暴力递归 解法二 动态规划 动态规划专题 leetcode1049. 最后一块石头的重量 I ...
- LeetCode-1049. 最后一块石头的重量 II
目录 思路 回溯法 动态规划 动态规划(压缩) 题目来源 1049. 最后一块石头的重量 II 思路 最后一块石头的重量,两个近似的石头值相近,那么最后一块石头的重量最小 举例:stones = [2 ...
- leetcode1049. 最后一块石头的重量 II
1.题目描述: 有一堆石头,用整数数组stones表示.其中stones[i]表示第i块石头的重量.每一回合,从中选出任意两块石头,然后将它们一起粉碎.假设石头的重量分别为x和y,且x <= y ...
- LeetCode-动态规划背包题-1049. 最后一块石头的重量 II
描述 1049. 最后一块石头的重量 II 有一堆石头,用整数数组 stones 表示.其中 stones[i] 表示第 i 块石头的重量. 每一回合,从中选出任意两块石头,然后将它们一起粉碎.假设石 ...
- LeetCode 1049. 最后一块石头的重量 II
1049. 最后一块石头的重量 II 无论按照何种顺序粉碎石头,最后一块石头的重量总是可以表示成 可以这样理解,将所有的石头分为两堆,ki=1的石头是一堆,ki=-1的石头是另一堆,我们的目标就是求上 ...
- 【5.31 代随_43day】 最后一块石头的重量 II、目标和、一和零
最后一块石头的重量 II.目标和.一和零 最后一块石头的重量 II 1.方法 图解步骤![在这里插入图片描述](https://img-blog.csdnimg.cn/d2266317bc43491f ...
- 最后一块石头的重量 II
最后一块石头的重量 II 有一堆石头,用整数数组 stones 表示.其中 stones[i] 表示第 i 块石头的重量. 每一回合,从中选出任意两块石头,然后将它们一起粉碎.假设石头的重量分别为 x ...
- LeetCode149、最后一块石头的重量 II
1049最后一块石头的重量 II 文章目录 1049最后一块石头的重量 II 题目链接 题目描述 解题思路 代码实现 总结 题目链接 https://leetcode-cn.com/problems/ ...
- day43|● 1049. 最后一块石头的重量 II ● 494. 目标和 ● 474.一和零
1049. 最后一块石头的重量 II 1.代码 class Solution { public:int lastStoneWeightII(vector<int>& stones) ...
最新文章
- 二分搜索 POJ 2456 Aggressive cows
- xshell启动报nssock2_nssock2.dll,下载,简介,描述,修复,等相关问题一站搞定_DLL之家
- 《CSS 禅意花园》读书笔记1
- nginx 配置文件 linux,Linux-nginx.conf配置文件模板
- linux删除第二次出现的字符,linux下 怎样删除文件名中包含特殊字符的文件
- 在Python中查找子字符串索引的5种方法
- PHP扩展库PEAR被攻击,近半年下载者或被影响
- 蓝色妖姬T3300摄像头有驱动无法显示画面解决方案
- 计算机组成基础(2)-- 微体系结构层
- 【bsauce读论文】 Playing for K(H)eaps: Understanding and Improving Linux Kernel Exploit Reliability
- 汉语语法研究参考文献
- 如何通过关键字和搜索结果分析用户需求
- Python:语音处理,实现在线朗读RFC文档或本地文本文件
- 模糊查询银行卡号mysql_mysql like查询字符串示例语句
- 并发编程——线程协作
- js计算两个时间戳之间的时间差(多少天、时、分、秒)
- 深圳二手房房价分析及预测
- 第三方支付创新与风控是未来关键——拉卡拉支付
- python decimal 转换为float_在Python中将float转换为decimal类型
- 重排九宫(广度优先算法)
热门文章
- osqa mysql_osqa安装出现的问题解决办法 | 学步园
- 理光m2554进入维修_理光DX2432C,基士得耶6201供墨检测代码,看完马上解决代码故障...
- 恢复初始快捷键_如何将Windows10系统还原初始状态
- python定义字典对象时_Python对象类型之字典
- 2021计算机科学调剂,2021北京科技大学计算机科学与技术专业接收调剂研究生的通知...
- linux中更改用户密码_如何在Linux中更改用户密码
- android传感器_Android传感器
- java方法_Java方法
- easymock接口模拟_EasyMock好又严格的模拟
- Python – numpy.linspace()