当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢
Xgboost报错ValueError:无效的形状:标签(1650 2)
[Dataset][VOC] Eyewear dataset 6000 in VOC format
Leading the demand and justifying the HR value - the successful launch of the "Human Resource Leading Model HRLM"
GCC编译器技术解析
The second day HCIP
HCIP 第三天实验
abaqus如何快速导入其他cae文件的assembly?
File upload vulnerability (2)
MySQL driver jar package download -- nanny tutorial
ASP.NET Core Web API 幂等性
随机推荐
The second day HCIP
两篇不错的php debug教程
Reverse resolve dns server
Vscode connect to remote server "Acquiring the lock on the/home / ~ 'problem
Nodejs安装教程
awk语法-01-基础语法(命令、选项、内部变量)
MySql 5.7.38下载安装教程 ,并实现在Navicat操作MySql
MySQL Advanced SQL Statements (2)
Toolbox App 1.25 New Features at a Glance | Version Update
(Part of it is not understood, and the notes are not completed) [Graph Theory] Difference Constraints
HCIP 第二天
速看!PMP新考纲、PMBOK第七版解读
HCIP 第三天实验
.NET静态代码织入——肉夹馍(Rougamo) 发布1.1.0
Specified URL is not reachable,caused by :‘Read timed out
交换网络----三种生成树协议
optional
Kind of weird!Access the destination URL, the host can container but not
MySQL Advanced Statements (1)
解决C#非静态字段、方法或属性“islandnum.Program.getIslandCount(int[][], int, int)”要求对象引用