LeetCode 1046. Last Stone Weight
原题链接在这里:https://leetcode.com/problems/last-stone-weight/
题目:
We have a collection of rocks, each rock has a positive integer weight.
Each turn, we choose the two heaviest 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 weight of this stone (or 0 if there are no stones left.)
Example 1:
Input: [2,7,4,1,8,1]
Output: 1
Explanation:
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone.
Note:
1 <= stones.length <= 30
1 <= stones[i] <= 1000
题解:
Put all stones into max heap.
While heap size >= 2, poll top 2 elements and get diff. If diff is larger than 0, add it back to heap.
Time Complexity: O(nlogn). n = stones.length. while loop could run for maximumn n-1 times.
Space: O(n).
AC Java:
class Solution {
public int lastStoneWeight(int[] stones) {
if(stones == null || stones.length == 0){
return 0;
} PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
for(int stone : stones){
maxHeap.add(stone);
} while(maxHeap.size() > 1){
int x = maxHeap.poll();
int y = maxHeap.poll();
int diff = x-y;
if(diff > 0){
maxHeap.add(diff);
}
} return maxHeap.isEmpty() ? 0 : maxHeap.peek();
}
}
LeetCode 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 ...
- LeetCode 1046. 最后一块石头的重量(1046. Last Stone Weight) 50
1046. 最后一块石头的重量 1046. Last Stone Weight 题目描述 每日一算法2019/6/22Day 50LeetCode1046. Last Stone Weight Jav ...
- 【Leetcode_easy】1046. Last Stone Weight
problem 1046. Last Stone Weight 参考 1. Leetcode_easy_1046. Last Stone Weight; 完
- LeetCode 1049. Last Stone Weight II
原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/ 题目: We have a collection of rocks, each ...
- 【LeetCode】1046. Last Stone Weight 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆 日期 题目地址:https://leetco ...
- 【leetcode】1046. Last Stone Weight
题目如下: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose t ...
- leetcode 1049 Last Stone Weight II(最后一块石头的重量 II)
有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出任意两块石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == y,那么 ...
- leetcode_1049. Last Stone Weight II_[DP]
1049. Last Stone Weight II https://leetcode.com/problems/last-stone-weight-ii/ 题意:从一堆石头里任选两个石头s1,s2, ...
- [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. ...
随机推荐
- Java开发笔记(一百四十二)JavaFX的对话框
JavaFX的对话框主要分为提示对话框和文件对话框两类,其中提示对话框又分作消息对话框.警告对话框.错误对话框.确认对话框四种.这四种对话框都使用Alert控件表达,并通过对话框类型加以区分,例如Al ...
- yzoj 2372 小B的数字 题解
题意 判断是否存在一个序列 $ b_i $ 使得 $ \prod_{i = 1}^{n} b_i | b_i^{a_i}$ 恒成立,其中 $ b_i $ 中的每个数都是2的正整数次幂. 样例输入 3 ...
- Delphi快递鸟【支持快递查询和单号识别】
作者QQ:(648437169) 点击下载➨Delphi快递鸟 [delphi快递鸟]支持快递查询.单号识别.
- 列表,元组以及range
列表,元组以及range 一.列表(list) 列表是数据类型之一,它有序,可变,支持索引 作用:存储数据,支持的数据类型很多:字符串,数字,布尔值,列表等 # 定义一个列表 lst = ['alex ...
- k8s集群node节点一直NotReady, 且node节点(并非master)的kubelet报错:Unable to update cni config: No networks found in /etc/cni/net.d
若要转载本文,请务必声明出处:https://www.cnblogs.com/zhongyuanzhao000/p/11401031.html 问题: 集群搭建的过程中,master节点初始化成功,但 ...
- Redis 集群:CLUSTERDOWN The cluster is down
1.错误 (error)CLUSTERDOWN The cluster is down 2.问题表现 Java项目使用redis集群时报错, HTTP Status 500 - Could not g ...
- FORM表单 onclick()与onsubmit()
FORM表单中onclick().submit()与onsubmit()的问题 最近遇到一次处理form数据的过滤,采用了button的onclick事件来检查,发现return false后表单仍然 ...
- python 代码中log表示含义
log表示以e为底数的对数函数符号.其验证代码如下: a=np.log(np.e )print(a)print(np.e)
- 【开发笔记】- 在MySQL中 root账户被锁定怎么办
MySQL的账户被锁定怎么办? 用Navicat连接数据库报错如下: Access denied for user 'root'@'localhost' (using password:YES) 原因 ...
- 【强烈推荐】ok-admin 一个好看又好用的后台模版!!!
ok-admin 一个很赞的,扁平化风格的,响应式布局的后台管理模版,旨为后端程序员减压! 目前一共有两个版本:ok-admin v1.0和ok-admin v2.0可自由选择! 源码地址:https ...