当前位置:网站首页>LeetCode_2357_使数组种所有元素都等于零
LeetCode_2357_使数组种所有元素都等于零
2022-08-02 08:19:00 【Fitz1318】
题目链接
题目描述
给你一个非负整数数组 nums 。在一步操作中,你必须:
- 选出一个正整数
x,x需要小于或等于nums中 最小 的 非零 元素。 nums中的每个正整数都减去x。
返回使 nums 中所有元素都等于 0 需要的 最少 操作数。
示例 1:
输入:nums = [1,5,0,3,5]
输出:3
解释:
第一步操作:选出 x = 1 ,之后 nums = [0,4,0,2,4] 。
第二步操作:选出 x = 2 ,之后 nums = [0,2,0,0,2] 。
第三步操作:选出 x = 2 ,之后 nums = [0,0,0,0,0] 。
示例 2:
输入:nums = [0]
输出:0
解释:nums 中的每个元素都已经是 0 ,所以不需要执行任何操作。
提示:
1 <= nums.length <= 1000 <= nums[i] <= 100
解题思路
- 具有相同值的元素将始终采用相同数量的操作才能变为 0。相反,具有不同值的元素将始终采用不同数量的操作才能变为 0。
- 对于数组中的元素,如果有
x个不同的且不等于0的元素,需要减x次即可
AC代码
class Solution {
public int minimumOperations(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int i : nums) {
set.add(i);
}
if (set.contains(0)) {
return set.size() - 1;
} else {
return set.size();
}
}
}
边栏推荐
- Redisson实现分布式锁
- Seleniu截图代码以及给图片赋值名字
- Flink 监控指南 被动拉取 Rest API
- 【论文阅读】Distilling the Knowledge in a Neural Network
- Analysis of software testing technology How far is Turing test from us
- Technology Cloud Report: To realize the metaverse, NVIDIA starts from building an infrastructure platform
- next permutation
- mysql 中 in 的用法
- Biotinyl Cystamine|CAS:128915-82-2|生物素半胱胺
- Business Intelligence Platform BI Business Intelligence Analysis Platform How to Choose the Right Business Intelligence Platform BI
猜你喜欢

HCIP笔记第十三天

OneNote 教程,如何在 OneNote 中创建更多空间?

houdini 求出曲线的法向 切线以及副法线

Redisson实现分布式锁

etcd implements large-scale service governance application combat

Biotin hydrazide HCl|CAS:66640-86-6|生物素-酰肼盐酸盐

Biotin-C6-amine|N-生物素基-1,6-己二胺|CAS:65953-56-2
What is NoSQL?Databases for the cloud-scale future

prometheus监控mysql_galera集群

Biotin-C6-amine|N-biotinyl-1,6-hexanediamine|CAS: 65953-56-2
随机推荐
prometheus monitoring mysql_galera cluster
day_05_pickel 和 json
【特别提醒】订阅此专栏的用户请先阅读本文再决定是否需要购买此专栏
Wang Xuegang - compiled shipment line file
下一个排列
tf.where使用
Installation and use of pnpm
构建Flink第一个应用程序
The packet capture tool Charles modifies the Response step
next permutation
用C写小游戏(三子棋)
Database triggers and transactions
C Language Basics_Union
OneNote 教程,如何在 OneNote 中创建更多空间?
RetinaFace: Single-stage Dense Face Localisation in the Wild
在 QT Creator 上配置 opencv 环境的一些认识和注意点
PyCharm使用教程(较详细,图+文)
Redisson实现分布式锁
知识点滴 - 为什么一般不用铜锅做菜
R language plotly visualization: plotly visualizes the scatter plot of the actual value of the regression model and the predicted value of the regression, analyzes the prediction performance of the re