当前位置:网站首页>2022.07.31(LC_6132_使数组中所有元素都等于零)
2022.07.31(LC_6132_使数组中所有元素都等于零)
2022-08-02 06:18:00 【Leeli9316】

方法一:优先队列
class Solution {
public int minimumOperations(int[] nums) {
Queue<Integer> pq = new PriorityQueue<>();
for (int num : nums) {
pq.offer(num);
}
int ans = 0;
while (!pq.isEmpty()) {
int min = pq.poll();
if (min != 0) {
List<Integer> list = new ArrayList<>();
while (!pq.isEmpty()) {
list.add(pq.poll() - min);
}
for (int num : list) {
pq.offer(num);
}
ans++;
}
}
return ans;
}
}方法二:哈希表
class Solution {
public int minimumOperations(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
if (num != 0) {
set.add(num);
}
}
return set.size();
}
}边栏推荐
- 笔记本开机黑屏提示:ERROR 0199:System Security-Security password retry count exceeded
- 文件上传漏洞(二)
- SphereEx苗立尧:云原生架构下的Database Mesh研发实践
- typescript 'props' is declared but its value is never read solution
- APP special test: traffic test
- Resolving C# non-static field, method or property "islandnum.Program.getIslandCount(int[][], int, int)" requires an object reference
- MySQL (3)
- 【21天学习挑战赛】顺序查找
- 交换部分 VLAN
- 实验7 MPLS实验
猜你喜欢

MySql 5.7.38 download and installation tutorial, and realize the operation of MySql in Navicat

CAT1 4G+以太网开发板腾讯云手机微信小程序显示温度和下发控制

实例031:字母识词

How does abaqus quickly import the assembly of other cae files?

MySQL Advanced Statements (1)

SphereEx苗立尧:云原生架构下的Database Mesh研发实践

MySQL - 多表查询与案例详解

The stock price has repeatedly hit new lows, and the real estate SaaS giant is in trouble. How should Mingyuan Cloud transform and save itself?

MySQL 23 classic interviews hang the interviewer

See the picture to understand | How to choose sales indicators to measure the health of business growth
随机推荐
The second day HCIP
CAT1 4G+Ethernet development board Tencent cloud mobile phone WeChat applet display temperature and delivery control
Unity Shader学习(七)纹理图像的简单使用
【暑期每日一题】洛谷 P1255 数楼梯
笔记本开机黑屏提示:ERROR 0199:System Security-Security password retry count exceeded
odoo field 设置匿名函数domain
MySQL Advanced Study Notes
Wuhan 2022 organizing of the high-performance computing added new ecological development of high-performance computing
MySql 5.7.38 download and installation tutorial, and realize the operation of MySql in Navicat
速看!PMP新考纲、PMBOK第七版解读
File upload vulnerability (2)
解决:- SPY: No data found for this date range, symbol may be delisted报错
About the local server problem after ue4.27 pixel streaming package
交换--STP协议
System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志。不可 访问的日志: Security
MySQL high-level --- storage engine, index, lock
交换部分 VLAN
2022年8月计划,着重ue4视频教程
Connection reset by peer 问题解析
Servlet