leetcode 1046
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的更多相关文章
- LeetCode 1046. 最后一块石头的重量(1046. Last Stone Weight) 50
1046. 最后一块石头的重量 1046. Last Stone Weight 题目描述 每日一算法2019/6/22Day 50LeetCode1046. Last Stone Weight Jav ...
- LeetCode 1046. Last Stone Weight
原题链接在这里:https://leetcode.com/problems/last-stone-weight/ 题目: We have a collection of rocks, each roc ...
- 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 ...
- LeetCode 1046. 最后一块石头的重量 (贪心)
有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出两块最重的石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == y,那 ...
- Leetcode题解 - 贪心算法部分简单题目代码+思路(860、944、1005、1029、1046、1217、1221)
leetcode真的是一个学习阅读理解的好地方 860. 柠檬水找零 """ 因为用户支付的只会有5.10.20 对于10元的用户必须找一个5 对于20元的用户可以找(三 ...
- 【LeetCode】1046. Last Stone Weight 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆 日期 题目地址:https://leetco ...
- 【leetcode】1046. Last Stone Weight
题目如下: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose t ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode.1046-最后的石头重量(Last Stone Weight)
这是小川的第388次更新,第418篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第250题(顺位题号是1046).有一个石头集合,每个石头都有一个正整数重量值. 每次,我 ...
随机推荐
- Promise 配合 axios 使用
Promise是一个构造函数,自己身上有all.reject.resolve这几个眼熟的方法,原型上有then.catch等同样很眼熟的方法 很细致的Promise使用详解 自己脑补 vue 工程化的 ...
- 安装Redis(Windows版本&Linux版本)
1.版本: Redis官网上有Linux版本,Redis官网:https://redis.io/download GitHub上有Windows版本,地址是:https://github.com/Mi ...
- robotframework执行自动化不能转换为h5页面的问题解决
电脑换成win10后,搭建了robotframework环境,执行自动化发现页面不支持h5页面了.请教了大佬,解决办法如下: 1.切换到DOS环境下,执行pip list命令,查看selenium2l ...
- 计算机二级考试:Java
目录 第 1 章 Java 语言概论 第 2 章 基本数据类型 2.1 概述 2.1.1 标识符 2.1.2 关键字 2.1.3 常量 2.2 基本数据类型 第 3 章 运算符和表达式 3.2 算术运 ...
- CentOS下MYSQL数据库的主从备份配置
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/limingzhong198/articl ...
- 「BalticOI 2020」病毒
AC自动机+DP最短路转移 怎么说呢,挺套路的,也不是太难,但是一上手会被大量的信息淹没思路,还是要注意关注主要信息,不要被一些细节卡住 由于抗体是要在基因序里面出现过,那么考虑把抗体的序列检出AC自 ...
- MapReduce在Shuffle阶段按Mapper输出的Value进行排序
ZKe ----------------- 在MapReduce框架中,Mapper的输出在Shuffle阶段,根据Key值分组之后,还将会根据Key值进行排序,因此Reducer的输出我们看到的结果 ...
- ner处理数据的方式
ner处理数据的方式biodef load_data(filename): features = [] labels = [] f = open(filename, encoding='utf-8') ...
- 消息队列--ActiveMQ集群部署
一.activeMQ主要的部署方式? 1,默认的单机部署(kahadb) activeMQ默认的存储单机模式,如果配置文件不做修改,则默认使用此模式.以本地的kahadb文件的方式进行存储,性能完全依 ...
- 模拟微信小程序页面Page方法
1.依赖 a.jQuery b.angularjs 2.page.js文件 1 var Page = function (options) { 2 var myApp = angular.module ...