当前位置:网站首页>2022.08.01_Daily Question
2022.08.01_Daily Question
2022-08-05 11:54:00 【No. い】
2357. Make all elements in the array equal to zero
topic description
Gives you an array of non-negative integers nums .In one step, you must:
- Select a positive integer
x,xneeds to be less than or equal to the minimum ofnumsA non-zero element. Subtract nums.
x from each positive integer in Returns the minimum number of operands required to make all elements in nums equal to 0.
Example 1:
Input:nums = [1,5,0,3,5]
Output:3
Explanation:
The first operation: choose x = 1, then nums = [0,4,0,2,4].
Step 2: choose x = 2 , then nums = [0,2,0,0,2] .
Step 3: Choose x = 2 , then nums = [0,0,0,0,0] .
Example 2:
Input:nums = [0]
Output:0
Explanation: Each element in nums has beenis 0, so no action is required.
Tip:
1 <= nums.length <= 1000 <= nums[i] <= 100
coding
greedy -> Subtract the maximum value that can be subtracted as much as possible each time, which will cause each operation to end, and at most it can only change the same number as it to 0, because it is all in the array.All non-zero numbers need to be subtracted, so the difference between the two numbers cannot be canceled, so in fact, each number greater than 0 needs to perform a separate operation, so the number of different positive integers in nums is the leastNumber of operations
0, 1, 2, 3, 4, 50, 0, 1, 2, 3, 40, 0, 0, 1, 2, 30, 0, 0, 0, 1, 20, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0class Solution {public int minimumOperations(int[] nums) {Set<Integer> set = newspan> HashSet<>();Arrays.stream(nums).forEach(i -> {set.add(i);});return set.contains(0) ? set.size() - 1 : set.size();}}边栏推荐
- STM32H743IIT6学习笔记02——USART
- WingIDE 7.2.0 远程调试
- C语言例题-打印日历
- 丹尼尔·拉瑞莫(BM):EOS的主要开发者
- Visit GOPS Long Zhi booth, Forrester's latest report: "the Forrester Wave: the fourth quarter of 2021 enterprise service management report
- 普通二本毕业八年,京东就职两年、百度三年,分享大厂心得
- Five reasons why developers choose Klocwork, a static analysis tool for code quality, for software security
- Mysql8基础知识
- 2-2.基金的投资交易与结算
- 花的含义
猜你喜欢

莅临GOPS大会龙智展位,获取Forrester最新报告:《Forrester Wave:2021年第四季度企业服务管理报告》

没开发人员,接到开发物联网系统的活儿,干不干?

澳洲站:电吹风AS/NZS 60335.2.23: 2017 安全标准测试

Hands-on Deep Learning_GoogLeNet / Inceptionv1v2v3v4

Security Issues and Prevention in Web3

STM32H743IIT6学习笔记03——使用第三方组件FreeRTOS

内存问题难定位,那是因为你没用ASAN

碘乙酰胺在Desthiobiotin-Iodoacetamide试剂中的作用?

基于NSQ搭建高可用分布式消息队列

2022.08.01_每日一题
随机推荐
女人是这个世界上最美丽的生命
后缀自动机(SAM)——黑盒使用方案
Keras 分割网络自定义评估函数 - mean iou
796. 子矩阵的和
STM32H743IIT6学习笔记03——使用第三方组件FreeRTOS
多业务模式下的交易链路探索与实践
2022 CCF国际AIOps挑战赛决赛暨AIOps研讨会报名已开启
力扣330 按要求补齐数组(贪心)
[供应链·案例篇]疫情影响下的全球十大零售商都做了些什么
No developers, received a job to develop an IoT system, do you want to do it?
Keras 模型多输出 loss weight metrics 设置
详细剖析 Redis 三种集群策略
Qt::qcustomplot 和 qchart数据填充相关
JS 从零手写实现一个call、apply方法
todolist案列——原生js
面经汇总-社招-6年
Reflect中的方法
【分享】代码编程习惯:在多参数的方法的情况下需要对方法整理-即最多参数的方法为主要实现
2022.08.01_每日一题
官方发布·2022南京智博会定于10月份在新庄国展召开