当前位置:网站首页>【堆】小红的数组
【堆】小红的数组
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();
}
}边栏推荐
- Dart named parameter syntax
- [Search topic] After reading the inevitable BFS solution to the shortest path problem
- HCIP(15)
- 【愚公系列】2022年07月 Go教学课程 025-递归函数
- ICML2022 | Deep Dive into Permutation-Sensitive Graph Neural Networks
- Browser download shortcut to desktop (PWA)
- "Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up
- 树莓派 的 arm 版的 gcc 安装 和环境变量的配置
- 【搜索专题】看完必会的BFS解决最短路问题攻略
- 787. 归并排序
猜你喜欢

Which interpolation is better for opencv to zoom in and out??

2022-07-31: Given a graph with n points and m directed edges, you can use magic to turn directed edges into undirected edges, such as directed edges from A to B, with a weight of 7.After casting the m

认真对待每一个时刻

How to download the Keil package

Simulation of Active anti-islanding-AFD Active Anti-islanding Model Based on Simulink

开源许可证 GPL、BSD、MIT、Mozilla、Apache和LGPL的区别

初出茅庐的小李第113篇博客项目笔记之机智云智能浇花器实战(2)-基础Demo实现

【kali-信息收集】枚举——DNS枚举:DNSenum、fierce

初出茅庐的小李第114篇博客项目笔记之机智云智能浇花器实战(3)-基础Demo实现

【入门教程】Rollup模块打包器整合
随机推荐
leetcode6133. 分组的最大数量(中等)
Character encoding and floating point calculation precision loss problem
Hackers can how bad to what degree?
What is dynamic programming and what is the knapsack problem
黑客到底可以厉害到什么程度?
软件测试基础理论知识—用例篇
指定set 'execution.savepoint.path'后,重启flinksql报这个错是啥
787. Merge Sort
787. 归并排序
雪糕和轮胎
基于STM32设计的UNO卡牌游戏(双人、多人对战)
HCIP(14)
The 16th day of the special assault version of the sword offer
Difference Between Compiled and Interpreted Languages
带你体验一次类型编程实践
Dynamic Programming 01 Backpack
This article takes you to understand the past and present of Mimir, Grafana's latest open source project
Basic Theoretical Knowledge of Software Testing - Use Cases
一个service层需要调用另两个service层获取数据,并组装成最后的数据,数据都是list,缓存如何设计?
【愚公系列】2022年07月 Go教学课程 024-函数