增加序列C

Increasing Sequence C++

本文关键字:增加      更新时间:2023-10-16

我尝试在代码战上解决这一挑战,但是它行不通。我最好的解决方案获得了25/26(在上次测试中超过了时间限制),但我删除了这一点,因为我昨天尝试过(o(n^2))。现在,我尝试了O(n)中的新产品。我很累,我真的很想今天完成此操作,所以请帮助我。

这是说明:给定一系列整数作为数组,请确定是否可以通过从数组中删除一个元素来获得严格增加的序列。

示例

For sequence = [1, 3, 2, 1], the output should be
almostIncreasingSequence(sequence) = false;
There is no one element in this array that can be removed in order to get a strictly increasing sequence.
For sequence = [1, 3, 2], the output should be
almostIncreasingSequence(sequence) = true.
You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].

这是我到现在为止的代码...(糟糕的代码):

#include <iostream>
#include <vector>
#include <algorithm>
bool almostIncreasingSequence(std::vector<int> sequence) 
{
    int count = 0;

    for(int i = 0; i < sequence.size()-1; i++)
    {
        if(sequence[i] > sequence[i+1])
        {
            count++;
            sequence.erase(sequence.begin(), sequence.begin() + i);
            i--;
        }
        if(count == 2)
            return false;
    }
    return true;
}
int main()
{
    std::cout << std::endl;
    return 0;
}

这是带有O(n)运行时的C 11解决方案:

constexpr auto Max = std::numeric_limits<std::size_t>::max();
bool is_sorted_but_skip(const std::vector<int>& vec, std::size_t index = Max){
    auto Start = index == 0 ? 1 : 0;
    auto prev = vec[Start];
    for(std::size_t i = Start + 1; i < vec.size(); i++){
        if(i == index) continue;
        if(prev >= vec[i]) return false;
        prev = vec[i];
    }
    return true;
}
bool almostIncreasingSequence(std::vector<int> v) 
{
    auto iter = std::adjacent_find(v.begin(), v.end(), [](int L, int R){ return L >= R; });
    if(is_sorted_but_skip(v, std::distance(v.begin(), iter)))
        return true;
    return is_sorted_but_skip(v, std::distance(v.begin(), std::next(iter)));
}

我们使用std::adjacent_find查找第一个元素,iter大于或等于其下一个元素。然后,我们在跳过iter的位置时严格对序列进行检查。

否则,我们在跳过iter+1的位置

时检查序列是否严格排序

案例复杂性较差:3线性扫描

demo

这是一个提示(嗯,几乎是一个解决方案):

如果您看到一个元素之间的减少,则必须删除其中一个(*)。

现在,如果您发现两对元素之间的两对不相交,该怎么办?是的: - )

牢记这一点,您应该能够使用线性扫描和一些恒定的工作来解决问题。

(*)排除第一对和最后一对元素。

这仍然是O(n^2),因为您在每次迭代中删除了向量的第一个元素。不要删除第一个元素,也不要在循环中 i--

如果您必须删除这些数字(您没有,但仍然如此),至少从列表的末尾进行。这样,删除一个数字可能是可能的 o(1)操作(我不确定这是实现std :: vector的方式)。

您真的不必删除数字。

#include<iostream>
#include<vector>
using namespace std;
int almostIncreasingSequence( vector<int> sequence );
int main(){
    int array[] = {40, 50, 60, 10, 20, 30};
    std::vector<int> vect (array, array + sizeof(array) / sizeof(int) );
    bool ret = almostIncreasingSequence(vect);
    if( ret ){
        std::cout<<"Array is strictly increasing.";
    }
    else{
std::cout<<"Array is not strictly increasing.";
    }
    return 0;
}
bool almostIncreasingSequence(std::vector<int> sequence) {
    int val = 0;
    int currentBig = sequence.at(0);
    for (int i = 1; i < sequence.size(); i++){
        if( currentBig < sequence.at(i))
        {
            currentBig = sequence.at(i);
        }
        else{
            val++;
            if( val>1)
            {
                return false;
            }
            if( i > 1 ){
                if (sequence.at(i) > sequence.at(i-2)){
                    if( currentBig < sequence.at(i) ){
                    }
                    else{
                        currentBig = sequence.at(i);
                    }
                }
            }
            else{
                currentBig = sequence.at(i);
            }
        }
    }
    return true;
}