当前位置:网站首页>6132. 使数组中所有元素都等于零-快速排序法
6132. 使数组中所有元素都等于零-快速排序法
2022-08-01 23:08: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;
}
边栏推荐
- excel remove all carriage return from a cell
- 简单3D渲染器的制作
- PHP算法之电话号码的字母组合
- 移动端人脸风格化技术的应用
- Small application project works WeChat stadium booking applet graduation design of the finished product (1) the development profile
- System availability: 3 9s, 4 9s in SRE's mouth... What is it?
- 文件查询匹配神器 【glob.js】 实用教程
- 浅析多服务在分布式系统下多事务通信处理机制方案
- C#大型互联网平台管理框架源码:基于ASP.NET MVC+EF6+Bootstrap开发,支持多数据库
- cmd指令
猜你喜欢
随机推荐
bat 之 特殊字符&转义
Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
prim生成树
下载安装 vscode(含汉化、插件的推荐和安装)
牛客多校4 A.Task Computing 思维
数据分析04
vscode hide menu bar
qt-faststart 安装使用
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D 题解
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D Solution
基于JAX的激活函数、softmax函数和交叉熵函数
SRv6 L3VPN的工作原理
How to use pywinauto and pyautogui to link the anime lady and sister please go home
分享10套开源免费的高品质源码,免费源码下载平台
Background project Express-Mysql-Vue3-TS-Pinia page layout-sidebar menu
Check if point is inside rectangle
小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
检查点是否在矩形内
10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
xctf attack and defense world web master advanced area webshell








