当前位置:网站首页>【堆】小红的数组
【堆】小红的数组
2022-08-01 03:45:00 【暮色_年华】
小红拿到了一个数组a ,每次操作小红可以选择数组中的任意一个数减去 ,小红一共能进行 k 次。
小红想在 k 次操作之后,数组的最大值尽可能小。请你返回这个最大值。
每次必须选择最大的减去,否则,最大值不会变,那么这次操作没有收益。
所以可以使用最大堆,可以自动维护数组的最大值。
建堆后,出堆k次并更新即可。
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param a int整型ArrayList
* @param k int整型
* @param x int整型
* @return int整型
*/
public int minMax (ArrayList<Integer> a, int k, int x) {
// write code here
PriorityQueue<Integer>q=new PriorityQueue<>(Collections.reverseOrder());
for(int i=0;i<a.size();i++){
q.add(a.get(i));
}
for(int i=0;i<k;i++){
int n=q.poll();
n-=x;
q.add(n);
}
return q.peek();
}
}
边栏推荐
- MySQL3
- 【kali-信息收集】枚举——DNS枚举:DNSenum、fierce
- Error using ts-node
- "ArchSummit: The cry of the times, technical people can hear"
- /etc/fstab
- Guys, MySQL cdc source recycles replication slave and r in incremental process
- Weekly Summary (*67): Why not dare to express an opinion
- pdb药物综合数据库
- 带你体验一次类型编程实践
- 带wiringPi库在unbutu 编译 并且在树莓派运行
猜你喜欢
Introduction to the Elastic Stack
[kali-information collection] enumeration - DNS enumeration: DNSenum, fierce
Article summary: the basic model of VPN and business types
Software Testing Weekly (Issue 82): In fact, all those who are entangled in making choices already have the answer in their hearts, and consultation is just to get the choice that they prefer.
Introduction to Oracle
纽约大学等 | TM-Vec:用于快速同源检测和比对的模版建模向量
这个地图绘制工具太赞了,推荐~~
The fledgling Xiao Li's 112th blog project notes: Wisdom cloud intelligent flower watering device actual combat (1) - basic Demo implementation
HCIP(15)
button去除黑框
随机推荐
Unity在BuildIn渲染管线下实现PlanarReflection的初级方法
785. 快速排序
Flutter "Hello world" program code
JS new fun(); 类与实例 JS基于对象语言 只能通过书写构造函数充当类
【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
ARM 交叉编译
认真对待每一个时刻
初出茅庐的小李第113篇博客项目笔记之机智云智能浇花器实战(2)-基础Demo实现
Dynamic Programming 01 Backpack
Basic use of vim - command mode
一个service层需要调用另两个service层获取数据,并组装成最后的数据,数据都是list,缓存如何设计?
Advice given by experts with four years of development experience in Flutter tutorial
MLP neural network, GRNN neural network, SVM neural network and deep learning neural network compare and identify human health and non-health data
Message queue MySQL table for storing message data
简单易用的任务队列-beanstalkd
HCIP(14)
New York University et al | TM-Vec: Template Modeling Vectors for Rapid Homology Detection and Alignment
【uniCloud】云对象的应用与提升
pdb药物综合数据库
787. 归并排序