当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Prufer sequence

Postman batch test interface detailed tutorial

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

请问什么是 CICD

Use Jenkins for continuous integration, this knowledge point must be mastered

系统可用性:SRE口中的3个9,4个9...到底是个什么东西?

D - Linear Probing- 并查集

域名重定向工具 —— SwitchHosts 实用教程

drf生成序列化类代码

xss相关知识点以及从 XSS Payload 学习浏览器解码
随机推荐
Always use "noopener" or "noreferrer" for links that open in a new tab
PHP算法之有效的括号
【数据分析03】
PDF转Word有那么难吗?做一个文件转换器,都解决了
CAKE:一个用于多视图知识图谱补全的可扩展性常识感知框架
perspectiveTransform warpPerspective getPerspectiveTransform findHomography
img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
Access the selected node in the console
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D 题解
E - Integer Sequence Fair
6133. 分组的最大数量
Use Jenkins for continuous integration, this knowledge point must be mastered
Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights
研发团队数字化转型实践
Chapter 11 Working with Dates and Times
D - Linear Probing- 并查集
TCP 可靠吗?为什么?
还在纠结报表工具的选型么?来看看这个
chrome复制一张图片的base64数据
将vim与系统剪贴板的交互使用