增加序列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;
}
相关文章:
- 数组索引的值没有增加
- 为什么我的代码在输出中增加了93天
- 有效地使用std::unordered_map来插入或增加键的值
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 为什么要增加导致崩溃的指针
- 增加传递地址的值
- 如何增加以前由新运算符分配的 C++ std::list 数组的大小?
- 为什么"i"在循环的每次迭代中都没有增加?(C++)
- std::thread 增加 DLL 引用计数,从而防止卸载 DLL
- 如何在C++中为增加但记住删除先前对象的对象分配唯一标识符
- 将一对插入地图并增加计数?
- 整数类型应该显式转换(例如"int"到"无符号")还是只会增加混乱?
- 我的 c++ 应用程序中的运行时间从 0 增加到 60 太快了(例如一毫秒或一微秒)
- 为什么当通过 TCP 发送的消息速率增加时,请求-响应消息对的延迟会降低?
- 琐碎并发代码的吞吐量不会随着线程数量的增加而增加
- C++:功能在输出前自动使用50减/增加输入数
- 追加位以增加字符的大小
- 为什么在C++中增加指针后打印了一个值而不是 NULL/0?
- 内存增加和字符串流?
- 如何增加使用 CUDA 实现的 FLOPS