当前位置:网站首页>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();
}
}边栏推荐
- 实验7 MPLS实验
- July 18-July 31, 2022 (Ue4 video tutorials and documentation, 20 hours. Total 1412 hours, 8588 hours left)
- At age 94, pioneer Turing award winner, computational complexity theory, Juris Hartmanis, died
- (笔记整理未完成)【图论】图的遍历
- 两篇不错的php debug教程
- Reverse resolve dns server
- MySql 5.7.38 download and installation tutorial, and realize the operation of MySql in Navicat
- Detailed explanation of 9 common reasons for MySQL index failure
- 数据库概论之MySQL表的增删改查2
- File upload vulnerability (2)
猜你喜欢

optional

HCIP day 3 experiment

Launch Space on-premises deployment (local) Beta!

数据库概论-MySQL的数据表的基本操作

Specified URL is not reachable,caused by :‘Read timed out

每周推荐短视频:为什么产品开发需要数字化?如何做到数字化?

交换部分 VLAN

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

The nacos source code can not find the istio package

APP special test: traffic test
随机推荐
MySQL classic 50 practice questions and the most detailed analysis of the whole network
HCIP 第二天
CAT1 4G+以太网开发板腾讯云手机微信小程序显示温度和下发控制
【暑期每日一题】洛谷 P1192 台阶问题
宝塔+FastAdmin 404 Not Found
HCIP 第三天实验
【21天学习挑战赛】顺序查找
Toolbox App 1.25 New Features at a Glance | Version Update
【npm install 报错问题合集】- npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
.NET Static Code Weaving - Rougamo Release 1.1.0
交换--STP协议
Expert Insights | 3 ways to seize innovation opportunities in a downturn
[npm install error report collection] - npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
MPLS的相关技术
MySQL Advanced SQL Statements
Pagoda+FastAdmin 404 Not Found
MySql -- 不存在则插入,存在则更新或忽略
Detailed explanation of 9 common reasons for MySQL index failure
ue先视频教程后深入
Xgboost报错ValueError:无效的形状:标签(1650 2)