Minimum no of changes required to make array strictly increasing

后端 未结 5 1770
栀梦
栀梦 2021-01-01 02:51

I have a problem in which we have an array of positive numbers and we have to make it strictly increasing by making zero or more changes to the array elements.

We ar

5条回答
  •  抹茶落季
    2021-01-01 03:32

    Changing Longest Increasing Sequence to accommodate for squeezing integral values between two successive increasing elements can ensure that we have integral values at appropriate places.

    Modified LIS code:

    int convert(vector v){
    vector lis(v.size(), 1);  
    for(int i = 1; i < v.size(); i++){   
        for(int j = 0; j < i; j++){  
            if(v[i] > v[j] && lis[i] < lis[j]+1 && (i-j) <= (v[i]-v[j]))   
                lis[i] = lis[j]+1;  
        }  
    }  
    int max_lis = 0;  
    for(int i = 0; i < v.size(); i++)  
        max_lis = max(max_lis, lis[i]);  
    int ans = v.size() - max_lis;
    return ans;
    }
    

提交回复
热议问题