原题链接在这里: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. Spring Boot 注解大全,真是太全了!

    一.注解(annotations)列表  @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguratio ...

  2. vue+element项目中 给input赋值之后无法修改

    点击修改按钮 将值赋值给 input 但是无法修改,input不可编辑,部分input可以编辑 , 解决方法一. 改变data数据初始值 解决方法二. 用this.$set input:{ descr ...

  3. python_封装redis_hash方法

    xshell 进入 虚拟环境 安装 redis workon py3env # 进入虚拟环境 pip install redis # 安装redis deactivate # 退出虚拟环境 简单的封装 ...

  4. 获取当前URL地址和$_GET获取参数

    用这个方法,可以在不使用$_get[]就可以获取get传过来的参数.还可以获取当前的URL public function getCurrentUrl() { $pageURL = 'http'; i ...

  5. MVC学习笔记(三)—用EF向数据库中添加数据

    1.在EFDemo文件夹中添加Controllers文件夹(用的是上一篇MVC学习笔记(二)—用EF创建数据库中的项目) 2.在Controllers文件夹下添加一个空的控制器(StudentsCon ...

  6. Java之路---Day08

    2019-10-22-22:28:39 目录 1.Static静态类 2.Static内存图 3.Static静态代码块 4.Arrays类 5.Math类 Static静态类 一旦使用static修 ...

  7. RHEL6搭建网络yum源软件仓库

    RHEL的更新包只对注册用户生效,所以需要自己手动改成Centos的更新包 一.查看rhel本身的yum安装包 rpm -qa | grep yum 二.卸载这些软件包 rpm -qa | grep ...

  8. 【openshift】OC命令部署Openshift

    OC命令部署Openshift # install openshift wget -c https://github.com/openshift/origin/releases/download/v3 ...

  9. Linux 命令:ls

    1. ls命令: list的缩写,查看目录中的文件 ls [选项] [目录名] dos 命令: dir 2. 常用参数: -a, –all 列出目录下的所有文件,包括以 . 开头的隐含文件 -A 同- ...

  10. 如何用JS获取地址栏参数的方法

    采用正则表达式获取地址栏参数: 写一个方法来进行正则匹配,同样也可以复用 function GetPar(name) { var reg = new RegExp("(^|&)&qu ...