当前位置:网站首页>2289. arrange arrays in non decreasing order

2289. arrange arrays in non decreasing order

2022-06-09 10:11:00 Mr Gao

2289. Make the array in non decreasing order

I'll give you a subscript from 0 The starting array of integers nums . In one step , Remove all satisfaction nums[i - 1] > nums[i] Of nums[i] , among 0 < i < nums.length .

Repeat steps , until nums Turn into The decreasing Array , Returns the number of operations to be performed .

Example 1:

Input :nums = [5,3,4,4,7,3,6,11,8,5,11]
Output :3
explain : Perform the following steps :

  • step 1 :[5,3,4,4,7,3,6,11,8,5,11] Turn into [5,4,4,7,6,11,11]
  • step 2 :[5,4,4,7,6,11,11] Turn into [5,4,7,11,11]
  • step 3 :[5,4,7,11,11] Turn into [5,7,11,11]
    [5,7,11,11] It is a group of non decreasing numbers , therefore , return 3 .

Example 2:

Input :nums = [4,5,7,7,13]
Output :0
explain :nums It is already a non decreasing group , therefore , return 0 .



int totalSteps(int* nums, int numsSize){
    
    int pre=nums[0];
    int i;
    int num=0;
   int size=numsSize;
   int r;
  
   while(1){
    
    int p=1;
    r=0;
    for(i=1;i<size;i++){
    
        
        if(nums[i]<nums[i-1]){
    
            r=1;
            continue;
        }
        else{
    
          
            nums[p++]=nums[i];
        }

    }
    num++;
    printf("%d ",p);
    if(r==0){
    
        break;
    }
    
    size=p;


   }
    return num-1;


}
原网站

版权声明
本文为[Mr Gao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090927455553.html