作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: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 weight x is totally destroyed, and the stone of weight y has new weight y-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. 1 <= stones.length <= 30
  2. 1 <= stones[i] <= 1000

题目大意

每次拿出两个最大的石头进行抵消。如果两个石头的重量一样,那么都粉碎;否则把大的石头粉碎掉和小的石头同样的重量,然后继续放到石头堆里。最后最多只会剩下一个石头,返回这个石头的重量或者0.

解题方法

大根堆

这个题还是挺经典的,不久前微软面试的时候问过类似的题目。

这个题就是我们在数据结构与算法中都学过的哈弗曼树的改变。哈弗曼树是指每次选择最小的两个元素合并成一个更大的元素,然后和剩下的一些元素继续重复这个操作。而这个题反其道而行之,每次选择最大的两个元素合并成一个较小的元素,然后和剩下的元素重复这个操作。

实现哈弗曼树最简单的方式就是使用堆。显然这个题使用大根堆。在python中的堆默认是小根堆,为了实现大根堆,一个方式是把所有的数字进行取反操作,最后的结果再取反即可。C++的堆默认是大根堆,那么我们就直接使用。

我觉得技术上的难点只有要进行多次的判断。一定要注意在进行pop的时候是否还有元素,特别是这个题要进行两次连续的pop。另外一个注意的点是如果两个石头抵消了,那么结果0就不一样放入原来的数组中了。最后一个注意的点事最后可能全部都抵消了,所以如果结果为空则返回0。

时间复杂度是O(NlogN)。

Python代码如下:

class Solution(object):
def lastStoneWeight(self, stones):
"""
:type stones: List[int]
:rtype: int
"""
stones = map(lambda x : -x, stones)
heapq.heapify(stones)
while len(stones) > 1:
x = heapq.heappop(stones)
if stones:
y = heapq.heappop(stones)
if x != y:
heapq.heappush(stones, -abs(x - y))
return 0 if not stones else -stones[0]

C++代码如下:

class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
priority_queue<int> q;
for (int s : stones)
q.push(s);
while (q.size() >= 2) {
int a = q.top(); q.pop();
if (!q.empty()) {
int b = q.top(); q.pop();
if (a != b) {
q.push(abs(a - b));
}
}
}
return q.empty() ? 0 : q.top();
}
};

日期

2019 年 6 月 8 日 —— 刷题尽量不要停

【LeetCode】1046. Last Stone Weight 解题报告(Python & C++)的更多相关文章

  1. LeetCode 1046. Last Stone Weight

    原题链接在这里:https://leetcode.com/problems/last-stone-weight/ 题目: We have a collection of rocks, each roc ...

  2. 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 ...

  3. 【LeetCode】877. Stone Game 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 双函数 单函数 + 记忆化递归 动态规划 日期 ...

  4. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  5. LeetCode 1046. 最后一块石头的重量(1046. Last Stone Weight) 50

    1046. 最后一块石头的重量 1046. Last Stone Weight 题目描述 每日一算法2019/6/22Day 50LeetCode1046. Last Stone Weight Jav ...

  6. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  7. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  8. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  9. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

随机推荐

  1. R 语言实战-Part 5-2笔记

    R 语言实战(第二版) part 5-2 技能拓展 ----------第21章创建包-------------------------- #包是一套函数.文档和数据的合集,以一种标准的格式保存 #1 ...

  2. BeautifulSoup解析库的介绍和使用

    ### BeautifulSoup解析库的介绍和使用 ### 三大选择器:节点选择器,方法选择器,CSS选择器 ### 使用建议:方法选择器 > CSS选择器 > 节点选择器 ## 测试文 ...

  3. rabbit mq的php使用 amqp 的支持

    rabbit mq的php使用 php想要操作rabbit 需要扩展amqp 1,先查看自己的php版本 phpinfo() 接下来下载dll文件 地址http://pecl.php.net/pack ...

  4. Git五个常见问题及解决方法

    一.删除远程仓库上被忽略的文件 由于种种原因,一些本应该被忽略的文件被我们误操作提交到了远程仓库了.那么我们该怎么删除这些文件呢? 以误提交了.idea目录为例,我们可以通过下面的步骤处理: 1)我们 ...

  5. 一道题目学ES6 API,合并对象id相同的两个数组对象

    var arr2=[{id:1,name:'23'}] var arr1=[{id:1,car:'car2'}] const combined = arr2.reduce((acc, cur) =&g ...

  6. Linux下删除的文件如何恢复

    Linux下删除的文件如何恢复 参考自: [1]linux下误操作删除文件如何恢复 [2]Linux实现删除撤回的方法 以/home/test.txt为例 1.df -T 文件夹 找到当前文件所在磁盘 ...

  7. 随录、EJB和JTA

    说道JTA(Java Transction Api),即事务的一种. 事务:说白了就是一组原子操作,是为了保证数据的安全性. 它,分为三类:JDBC事务,JTA事务,还有容器事务. JDBC是由Con ...

  8. SQL模糊查询语句和Escape转义字符

    通配符描述示例%包含零个或更多字符的任意字符串.WHERE title LIKE '%computer%' 将查找处于书名任意位置的包含单词 computer 的所有书名._(下划线)任何单个字符.W ...

  9. Linux基础命令---nfsstat显示nfs信息

    nfsstat nfsstat指令用来显示nfs客户端和服务器的活动信息. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法       nfsstat  ...

  10. 阿里云esc 安装 mysql5.7.27

    1. 下载:  wget  http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm 2. 安装: (1) yum -y in ...