当前位置:网站首页>6132. All the elements in the array is equal to zero - quick sort method
6132. All the elements in the array is equal to zero - quick sort method
2022-08-01 23:10:00 【Mr Gao】
6132. 使数组中所有元素都等于零
给你一个非负整数数组 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 ,所以不需要执行任何操作.
void quick_sort(int *a,int low,int high){
int l=low,h=high;
if(low<high){
int p=a[low];
while(low<high){
while(low<high&&a[high]>=p){
high--;
}
a[low]=a[high];
while(low<high&&a[low]<=p){
low++;
}
a[high]=a[low];
}
a[low]=p;
quick_sort(a,l,low-1);
quick_sort(a,low+1,h);
}
}
int minimumOperations(int* nums, int numsSize){
quick_sort(nums,0,numsSize-1);
int target=nums[0];
int count=0;
int i;
for(i=1;i<numsSize;i++){
if(nums[i]!=target){
count++;
target=nums[i];
}
}
// printf("%d ",count);
if(nums[0]!=0){
count=count+1;
}
return count;
}
边栏推荐
- Wechat Gymnasium Reservation Mini Program Graduation Design Finished Work Mini Program Graduation Design Finished Product (2) Mini Program Function
- vscode hide menu bar
- Additional Features for Scripting
- A. Doremy‘s IQ-- Codeforces Round #808 (Div. 1)
- 高效工作文档产出归类
- 分享10套开源免费的高品质源码,免费源码下载平台
- Deep learning Course2 first week Practical aspects of Deep Learning exercises
- Quarantine and downgrade
- Postman batch test interface detailed tutorial
- 问题解决方式了
猜你喜欢

Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (4) Opening Report

Postman batch test interface detailed tutorial

简单3D渲染器的制作

程序员如何优雅地解决线上问题?

How to add a game character to a UE4 scene

D - Linear Probing- 并查集

(Translation) How the contrasting color of the button guides the user's actions

关于ETL的两种架构(ETL架构和ELT架构)

DRF generating serialization class code

10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
随机推荐
chrome copies the base64 data of an image
移动端人脸风格化技术的应用
img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
13、学习MySQL 分组
excel vertical to horizontal
y84. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (15)
对于在新标签页中打开的链接,始终使用“noopener”或“noreferrer”
【好书推荐】第一本无人驾驶技术书
CF1703G Good Key, Bad Key
Chapter 19 Tips and Traps: Common Goofs for Novices
数据增强--学习笔记(图像类,cnn)
ROS2初级知识(8):Launching启动多节点
How to add a game character to a UE4 scene
加载字体时避免隐藏文本
System availability: 3 9s, 4 9s in SRE's mouth... What is it?
华为无线设备配置双链路冷备份(AP指定配置方式)
From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program
Postman batch test interface detailed tutorial
SQL Server (design database--stored procedure--trigger)
避免使用 <b>、<i>、<s> 和 <u> 标签