class Solution {
   
    public int lastStoneWeight(int[] stones) {
        MaxHeap s=new MaxHeap(stones.length+1);
        for(int i=0;i<stones.length;i++)
        {
            s.insert(stones[i]);
        }
        while(s.size()>1)
        {
            int y=s.extractMax();
            int x=s.extractMax();
            if(y>x)
            {
                s.insert(y-x);
            }
        }
        if(s.size()==1)
        {
            return s.extractMax();
        }
        else
            return 0;
        
    }
}
class MaxHeap{
    private int[] stone;
    private int count;
    private int capacity;
    public MaxHeap(int capacity)
    {
        this.capacity=capacity;
        stone=new int[capacity+1];
        count=0;
    }
    public void insert(int n)
    {
        stone[++count]=n;
        shiftUp(count);
    }
    private void shiftUp(int count)
    {
        while(count>1&&stone[count/2]<stone[count])
        {
            swap(count/2,count);
            count=count/2;
        }
    }
    public void swap(int i,int j)
    {
        int temp=stone[i];
        stone[i]=stone[j];
        stone[j]=temp;
    }
    public int extractMax()
    {
        int res=stone[1];
        swap(1,count);
        count--;
        shiftDown(1);
        return res;
    }
    private void shiftDown(int i)
    {
      int j=2*i;
      while(j<=count)
      {
          if(j+1<=count&&stone[j+1]>stone[j])
          {
              if(stone[j+1]>stone[i])
              {
                   swap(j+1,i);
                   i=j+1;
                   j=2*i;
              }
              else
                  break;
             
          }
          else
          {
              if(stone[j]>stone[i])
              {
                  swap(j,i);
                  i=j;
                  j=2*i;
              }
              else
                  break;
          }
      }
    }  
    public int size()
    {
        return count;
    }
}

leetcode 1046的更多相关文章

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

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

  2. LeetCode 1046. Last Stone Weight

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

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

  4. LeetCode 1046. 最后一块石头的重量 (贪心)

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

  5. Leetcode题解 - 贪心算法部分简单题目代码+思路(860、944、1005、1029、1046、1217、1221)

    leetcode真的是一个学习阅读理解的好地方 860. 柠檬水找零 """ 因为用户支付的只会有5.10.20 对于10元的用户必须找一个5 对于20元的用户可以找(三 ...

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

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

  7. 【leetcode】1046. Last Stone Weight

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

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. LeetCode.1046-最后的石头重量(Last Stone Weight)

    这是小川的第388次更新,第418篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第250题(顺位题号是1046).有一个石头集合,每个石头都有一个正整数重量值. 每次,我 ...

随机推荐

  1. 用c++获取随机数

    计算机的随机数都是由伪随机数,即是由小M多项式序列生成的,其中产生每个小序列都有一个初始值,即随机种子.(注意: 小M多项式序列的周期是65535,即每次利用一个随机种子生成的随机数的周期是65535 ...

  2. 创建Sqlite数据库(二)

    先创建一个数据库表,然后在主activity中执行删除更新操作 public class MainActivity extends AppCompatActivity { @Override prot ...

  3. Docker启动Mysql镜像

    date: 2020-03-14 17:00:00 updated: 2020-03-14 18:00:00 Docker启动Mysql镜像 管理员权限!!! docker run -p 3306:3 ...

  4. 对PatchGAN的感知域(receptive_field)理解

    for basic discriminator of GANs 判别器用于感知生成器产生的合成图片和ground-truth的差异,并旨在实现区分出fake or real: 同时,判别器的输出也是经 ...

  5. AtCoder Grand Contest 013D: Piling Up 题解

    题意简化: [luogu] Piling Up 一开始有n个颜色为黑白的球,但不知道黑白色分别有多少,m次操作,每次先拿出一个球,再放入黑白球各一个,再拿出一个球,最后拿出的球按顺序排列会形成一个颜色 ...

  6. uniApp朋友圈(参考)

    介绍 功能:回复,点赞(笔芯),评论,图片(最多六张). 码云地址:https://gitee.com/sunliusen/friend 例:

  7. MySQL全面瓦解6:查询的基本操作

    概述 提到查询,就回到我们第四篇的SQL语言分类了,DQL(Data QueryLanguage),也就是数据查询语言,实际就是从数据库中获取数据的一种命令方式.我们给数据库发送一个查询语句的命令,数 ...

  8. delphi key解密转c# 解决string 不可变长度问题

    遇见问题: delphi的解密需要在c#里面实现 方法一:delphi编写delphi dll组件,c#里面调用 方法二:c#重写delphi的代码进行解析 方法一: delphi部分代码: libr ...

  9. Jmeter(二十六) - 从入门到精通 - 搭建开源论坛JForum(详解教程)

    1.简介 今天这篇文章主要是给大家讲解一下,如何部署测试环境,这里宏哥部署一个开源测论坛,后边的文章中会用到这个论坛,并且也看到童鞋们在群里讨论如何在开发将测试包发给你以后,你如何快速地部署测试环境. ...

  10. String字符串加号的作用与基本数据类型加号的作用的区别

    1 public static void main(String args[] ){ 2 String Str="hellow"; 3 int num=110; 4 char c= ...