原题链接在这里: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

题解:

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();
}
}

跟上Last Stone Weight II.

LeetCode 1046. Last Stone Weight的更多相关文章

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

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

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

  3. 【Leetcode_easy】1046. Last Stone Weight

    problem 1046. Last Stone Weight 参考 1. Leetcode_easy_1046. Last Stone Weight; 完

  4. LeetCode 1049. Last Stone Weight II

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

  5. 【LeetCode】1046. Last Stone Weight 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆 日期 题目地址:https://leetco ...

  6. 【leetcode】1046. Last Stone Weight

    题目如下: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose t ...

  7. leetcode 1049 Last Stone Weight II(最后一块石头的重量 II)

    有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出任意两块石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == y,那么 ...

  8. leetcode_1049. Last Stone Weight II_[DP]

    1049. Last Stone Weight II https://leetcode.com/problems/last-stone-weight-ii/ 题意:从一堆石头里任选两个石头s1,s2, ...

  9. [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. ...

随机推荐

  1. Spark学习(2) RDD编程

    什么是RDD RDD(Resilient Distributed Dataset)叫做分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.弹性.里面的元素可并行计算的集合 RDD允 ...

  2. FORMAT 的用法

    https://www.cnblogs.com/gaodu2003/archive/2008/12/22/1359927.html Format 格式指令具有以下的形式:"%" [ ...

  3. 1. Spark GraphX概述

    1.1 什么是Spark GraphX Spark GraphX是一个分布式图处理框架,它是基于Spark平台提供对图计算和图挖掘简洁易用的而丰富的接口,极大的方便了对分布式图处理的需求.那么什么是图 ...

  4. 用LabVIEW做声源定位系统

    前一阵子,研发部举办了为期三天的第一届Innovation Day,让大家用3天时间去完成工作之外的一些创意.有人做微信小程序,有人继续研究一些工作中用到的Tool,有人把一直想解决而没时间解决的老b ...

  5. 【Oracle】获取SQL执行计划

    一.plsql developer工具F5         在sqldeveloper中选中sql按F5即可查看执行计划         

  6. C++编译器会对没有构造函数的类生成默认构造函数吗?(有必要的时候才生成,要看情况。有反汇编验证)

    之前在上C++的课的时候,印象中有那么一句话:如果一个类没有任何构造函数,那么编译器会生成一个默认的构造函数 今天在看<深度探索C++对象模型>的第二章:“构造函数语意学”的时候发现之前听 ...

  7. Spring-Cloud之Zuul路由网关-6

    一.为什么需要Zuul? Zuul 作为微服务系统的网关组件,用于构建边界服务( Edge Service ),致力于动态路由.过滤.监控.弹性伸缩和安全.Zuul 作为路由网关组件,在微服务架构中有 ...

  8. 利用onMouseOver和onMouseOut实现图像翻滚

    代码: <img src="images/001.jpg" alt="pic" onmouseover="this.src='images/00 ...

  9. Synchronized可重入锁分析

    可重入锁又称递归锁,是指在同一个线程在外层方法获取锁的时候,再进入该线程的内层方法会自动获取锁(前提是锁对象必须是同一对象或者class), 不会因为之前已经获取过还没实方而发生阻塞.即同一线程可执行 ...

  10. Mac版StarUML破解方法

    StarUML是用nodejs写的.确切的说是用Electron前端框架写的.新版本中所有的starUML源代码是通过asar工具打包而成.确切的代码位置在“%LOCALAPPDATA%\Progra ...