当前位置:网站首页>leetcode-6132: Make all elements in array equal to zero
leetcode-6132: Make all elements in array equal to zero
2022-08-01 07:58:00 【chrysanthemum bat】
leetcode-6132:Makes all elements in the array equal to zero
题目
给你一个非负整数数组 nums .在一步操作中,你必须:
pick a positive integer x ,x 需要小于或等于 nums 中 最小 的 非零 元素.
nums Subtract each positive integer in x.
返回使 nums All elements in are equal 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 Every element in is already 0 ,So no action is required.

解题
方法一:模拟
由于nums.length最大为100,So you can use brute force simulation to do this problem
class Solution {
public:
int minimumOperations(vector<int>& nums) {
int n=nums.size();
int res=0;
for(int i=0;i<n;i++){
sort(nums.begin(),nums.end());
if(nums[i]==0) continue;
for(int j=n-1;j>=i;j--){
nums[j]-=nums[i];
}
res++;
}
return res;
}
};
方法二:转化为 Find the number of non-zero and distinct elements
class Solution {
public:
int minimumOperations(vector<int>& nums) {
unordered_set<int> set;
for(int num:nums){
if(num!=0) set.insert(num);
}
return set.size();
}
};
边栏推荐
猜你喜欢

【Unity3D】相机

华为深度学习课程第六、七章

【手撕AHB-APB Bridge】~ AHB地址总线的低两位为什么不用来表示地址呢?

rhcsa 第四天

The log causes these pits in the thread block, you have to prevent
![[Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?](/img/fb/c95c5857024db001638cd484c5e78f.png)
[Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?

国内外最顶级的8大plm项目管理系统

leetcode-6135:图中的最长环

小程序全面屏手势配置案例

聊一聊ICMP协议以及ping的过程
随机推荐
XX市消防救援指挥中心实战指挥平台多链路聚合解决方案实例
SaaS安全认证综合指南
VoLTE基础学习系列 | 什么是SIP和IMS中的Forking
热修复技术可谓是百花齐放
Vim扩展内容
22牛客多校1 C.Grab the Seat (几何 + 暴力)
centos 安装php7.4,搭建hyperf,转发RDS
【一句话攻略】彻底理解JS中的回调(Callback)函数
特殊的日子,值得纪念
nodetype中值1、2、3分别代表什么意思
Data Analysis 5
图像基本操作的其他内容
Mysql数据库的部署以及初始化步骤
Chapters 6 and 7 of Huawei Deep Learning Course
Fist game copyright-free music download, League of Legends copyright-free music, can be used for video creation, live broadcast
【杭电多校第四场 B题】最短路图+缩点dp
Go supports OOP: use struct instead of class
SAP ABAP ALV+SMARTFORS 表分页 报表打印程序
22 Niu Ke Duo School 1 I. Chiitoitsu (Probability dp)
最新的Cesium和Three的整合方法(附完整代码)