我尝试解决CodeFights上的这个挑战,但它不起作用。我最好的解决方案得到了25/26(最后一次测试超出了时间限制),但我删除了,因为我昨天试过了(这是O(n^2))。现在我尝试了一个新的O(n)。我非常疲倦,我很想今天完成这个任务,所以请帮助我。增加序列C++
下面是语句: 给定一个整数序列作为数组,确定是否可以通过从数组中删除不多于一个元素来获得严格增加的序列。
例
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;
}
此代码现在没有(几乎)没有调用'almostIncreasingSequence'函数。 – ForceBru
这在挑战中并不重要,我必须只写功能,main和header会自动添加:)我写主+标题是因为我想将这些文件保存在我的电脑中 – Vader
请不要链接到需要登录或导航的网站你的代码。这个问题陈述是什么意思? – ThomasMcLeod