原题链接在这里: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. 列表,元组以及range

    列表,元组以及range 一.列表(list) 列表是数据类型之一,它有序,可变,支持索引 作用:存储数据,支持的数据类型很多:字符串,数字,布尔值,列表等 # 定义一个列表 lst = ['alex ...

  2. Locust性能测试-环境准备与基本使用 转自:悠悠

    前言 提到性能测试,大部分小伙伴想到的就是LR和jmeter这种工具,小编一直不太喜欢写这种工具类的东西,我的原则是能用代码解决的问题,尽量不去用工具. python里面也有一个性能测试框架Locus ...

  3. [Atcoder AGC030C]Coloring Torus

    题目大意:有$k$种颜色,要求构造出一个$n\times n$的矩阵,填入这$k$种颜色,满足对于每一种颜色,其中填充这种颜色的每一个方格,满足其相连的四个格子的颜色的个数和种类相同(对于每一种颜色而 ...

  4. 【简解】C2CRNI - Crni

    [题目大意] 给定一个N行N列的矩阵,每个格子要么为白色要么为黑色.黑矩形为所涵单元格数大于等于2且所涵单元格均为黑色的矩表.要解决的问题是在给定的矩形中找出两个没有共公部分的黑矩形,输出所有方案数, ...

  5. linux 释放系统内存命令

    1.sync 因为系统在操作的过程当中,会把你的操作到的文件资料先保存到buffer中去,因为怕你在操作的过程中因为断电等原因遗失数据,所以在你操作过程中会把文件资料先缓存.所以我们执行sync命令, ...

  6. linux 安装Python3.6

    1.安装依赖 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel ...

  7. spring中AspectJ的使用

    目录 AspectJ: AOP术语 通知的类型 切入点表达式 基于xml的AspectJ编程 导入jar包 定义切面类 引入约束 AOP配置 基于注解的AspectJ编程 AspectJ: 什么是AO ...

  8. win 修改notebook路径

    开始发现 notebook 默认的路径是 C:\Users\Administrator 需要修改 将目标中的%USERPROFILE% 直接删掉了

  9. requirejs:模块加载(require)及定义(define)时的路径理解

    给新来的实习生普及下JS基本知识,看到比较好的文章 转载https://blog.csdn.net/xuxiaoping1989/article/details/52384778 接触过require ...

  10. BFC 到底是什么?

    MDN 对 BFC 的描述: 块格式化上下文(Block Formatting Context,BFC) 是Web页面的可视化CSS渲染的一部分,是布局过程中生成块级盒子的区域,也是浮动元素与其他元素 ...