LeetCode 1187. 使数组严格递增

tle 但是我好开心啊(真)

#include<bits/stdc++.h>
using namespace std;

int srh(vector<int>& arr1, vector<int>& arr2, int a, int b)
{//搜索改完,搜完,一定要改回来再搜,保持之后的人还是原数组
    if(a>=arr1.size()) return 0;
    int ans = arr2.size()+1;
    int j = b;
    for(; j < arr2.size(); j++)
    {
        if(arr2[j] > arr1[a-1]) break;
    }
    if(j < arr2.size()) 
    {
        int t = arr1[a];
        arr1[a] = arr2[j];
        ans = srh(arr1, arr2, a+1, j+1)+1;
        arr1[a] = t;
    }
    if(arr1[a] > arr1[a-1]) ans = min(ans,srh(arr1, arr2, a+1, b));
    return ans;
}

int makeArrayIncreasing(vector<int>& arr1, vector<int>& arr2) {
    sort(arr2.begin(), arr2.end());
    vector<int>::iterator new_end = unique(arr2.begin(), arr2.end());//"删除"相邻的重复元素
    arr2.erase(new_end, arr2.end());//删除(真正的删除)重复的元素
    int t = arr1[0];
    arr1[0] = arr2[0];
    int ans = srh(arr1, arr2, 1, 1)+1;
    arr1[0] = t;
    ans = min(ans, srh(arr1, arr2, 1, 0));
    if (ans > arr2.size()) ans = -1;
    return ans;
}

int main(int argc, char const *argv[])
{
    int a[10] = {5,16,19,2,1,12,7,14,5,16};
    int b[15] = {6,17,4,3,6,13,4,3,18,17,16,7,14,1,16};
    vector<int> arr1(a, a+10);
    vector<int> arr2(b, b+15);
    int ans = makeArrayIncreasing(arr1, arr2);
    cout<<ans<<endl;
    return 0;
}
上一篇:【javascript】浅析js中的堆和栈


下一篇:自定义函数实现strcpy拷贝字符串