Find the longest increasing subsequence length

Input format:
input two lines: the first line: n, the number of columns of the second row representing the number to be entered: between the number n, the digital apart by spaces

Output format:
the longest sub monotonically increasing sequence length

Sample input:

Here we are given a set of inputs. For example:
. 5
. 1. 3 2. 5. 9

Sample output:

Given here corresponding output. For example:
4

#include<stdio.h>
#include<malloc.h>
int solution(int a[],int length)
{
	int max=0;
	int part_max=1;
	int *b=(int*)malloc(sizeof(int)*length);
	for(int i=0;i<length;i++)
	{
		b[i]=1;
	}
	for(int i=0;i<length;i++)
	{
		for(int j=0;j<i;j++)
		{
			if(a[j]<a[i])
			{
				b[i]=b[j]+1;
				if(b[i]>part_max)
				{
					part_max=b[i];
				}		
			}
		}
		b[i]=part_max;
		part_max=1;
		if(b[i]>max)
			max=b[i];
	}
	return max;
}
int main()
{
	int n,max;
	scanf("%d",&n);
	int *a=(int*)malloc(sizeof(int)*n);
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	max=solution(a,n);
	printf("%d",max);
}

 

Published 33 original articles · won praise 30 · views 20000 +

Guess you like

Origin blog.csdn.net/baidu_15547923/article/details/100547140