原题目: #面试编程题# Given a array of integers , find 3 indexes i,j,k such that, i<j<k and a < a
< a
. Could you find possible iO(n) algorithm.
来源:http://weibo.com/1915548291/zpuaLlfh5#_rnd1364438221023
使用LIS 找3个长度的递增序列
问题:不知道下标如何记录,第一个数只能循环一遍输出了。
代码如下所示:
#include <iostream>
#include <cassert>
#define max 500
using namespace std;
void find3(int *a, int *last,int length, int n)
{
assert(n<=length);
for(int i = 0; i<length; ++i)
{
for(int j = n-1; j>=0; --j)
{
if(a[i]>last[j])
{
last[j+1] = a[i];
if(j == n-1)
{
return;
}
break;
}
}
}
}
int main()
{
int last[4] ={0,max,max,max};
int a[10];
for(int i = 0;i<10; ++i)
{
cin>>a[i];
}
find3(a, last, 10, 3);
if(last[3] == max)
{
cout<<"no such sequence"<<endl;
return -1;
}
for(int i = 0; i<10; ++i)
if(a[i] < last[2])
{
cout<<a[i]<<endl;
break;
}
cout<<last[2]<<endl;
cout<<last[3]<<endl;
return 0;
}