当前位置:网站首页>【堆】小红的数组
【堆】小红的数组
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();
}
}边栏推荐
- Flutter Tutorial 02 Introduction to Flutter Desktop Program Development Tutorial Run hello world (tutorial includes source code)
- Ordinary users cannot access HGFS directory
- What is dynamic programming and what is the knapsack problem
- Character encoding and floating point calculation precision loss problem
- Elastic Stack的介绍
- Input输入框光标在前输入后自动跳到最后面的bug
- Flutter Tutorial 01 Configure the environment and run the demo program (tutorial includes source code)
- The maximum quantity leetcode6133. Grouping (medium)
- HIRO: Hierarchical Reinforcement Learning 】 【 Data - Efficient Hierarchical Reinforcement Learning
- leetcode6133. 分组的最大数量(中等)
猜你喜欢

New York University et al | TM-Vec: Template Modeling Vectors for Rapid Homology Detection and Alignment

HCIP (14)

最新 955 不加班的公司名单

Flink 1.13 (8) CDC

leetcode6132. 使数组中所有元素都等于零(简单,周赛)

Replacing the Raspberry Pi Kernel
![[kali-information collection] enumeration - DNS enumeration: DNSenum, fierce](/img/97/bbe7c2af0ff8bcb5222b9105d80c73.png)
[kali-information collection] enumeration - DNS enumeration: DNSenum, fierce

The maximum quantity leetcode6133. Grouping (medium)

How to promote new products online?

ARM cross compilation
随机推荐
Error using ts-node
最新 955 不加班的公司名单
带wiringPi库在unbutu 编译 并且在树莓派运行
JS new fun(); 类与实例 JS基于对象语言 只能通过书写构造函数充当类
Step by step hand tearing carousel Figure 3 (nanny level tutorial)
Weekly Summary (*67): Why not dare to express an opinion
leetcode:126. Word Solitaire II
[kali-information collection] enumeration - DNS enumeration: DNSenum, fierce
一个service层需要调用另两个service层获取数据,并组装成最后的数据,数据都是list,缓存如何设计?
/etc/fstab
Basic use of vim - command mode
"ArchSummit: The cry of the times, technical people can hear"
二舅
ARM 交叉编译
Message Queuing Message Storage Design (Architecture Camp Module 8 Jobs)
《少年派2》:新男友竟脚踩两只船,林妙妙与钱三一感情回温
HCIP(15)
测试
Open source project site must-have & communication area function
初出茅庐的小李第114篇博客项目笔记之机智云智能浇花器实战(3)-基础Demo实现