当前位置:网站首页>【堆】小红的数组
【堆】小红的数组
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();
}
}
边栏推荐
- leetcode6132. 使数组中所有元素都等于零(简单,周赛)
- JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
- Weekly Summary (*67): Why not dare to express an opinion
- By CSDN, torn
- button remove black frame
- 2. # code comments
- The kernel's handling of the device tree
- MySQL3
- Basic use of vim - command mode
- pdb drug comprehensive database
猜你喜欢
Flink 1.13 (8) CDC
IJCAI2022 | Hybrid Probabilistic Reasoning with Algebraic and Logical Constraints
最新 955 不加班的公司名单
The maximum quantity leetcode6133. Grouping (medium)
second uncle
【kali-信息收集】枚举——DNS枚举:DNSenum、fierce
Open source project site must-have & communication area function
"ArchSummit: The cry of the times, technical people can hear"
【搜索专题】看完必会的BFS解决最短路问题攻略
Introduction to Oracle
随机推荐
What is dynamic programming and what is the knapsack problem
Software Testing Interview (3)
785. 快速排序
HCIP (14)
HCIP(15)
Open source project site must-have & communication area function
这个地图绘制工具太赞了,推荐~~
Introduction to Oracle
This map drawing tool is amazing, I recommend it~~
Compiled on unbutu with wiringPi library and run on Raspberry Pi
MySQL4
The bigger and bigger the project is, I split it like this
Dart named parameter syntax
【入门教程】Rollup模块打包器整合
Nmap 操作手册 - 完整版
win10 固定本机IP
IJCAI2022 | Hybrid Probabilistic Reasoning with Algebraic and Logical Constraints
JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
Weekly Summary (*67): Why not dare to express an opinion
[uniCloud] Application and Improvement of Cloud Objects