File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed
DP/LongestIncreasingSubsequence Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+
4
+ typedef struct node {
5
+ int data ;
6
+ struct node * next ;
7
+ }node ;
8
+
9
+ node * append (node * head ,int d ){
10
+ node * temp = NULL ;
11
+ temp = (node * )malloc (sizeof (node ));
12
+ temp -> data = d ;
13
+ temp -> next = NULL ;
14
+ if (head == NULL ){
15
+ head = temp ;
16
+ return head ;
17
+ }
18
+ temp -> next = head ;
19
+ return temp ;
20
+ }
21
+
22
+ void printReverse (node * head ){
23
+ if (head -> next == NULL )
24
+ return ;
25
+ printReverse (head -> next );
26
+ printf ("%d " , head -> data );
27
+ }
28
+
29
+ int main (void ) {
30
+ int n ,i ,j ;
31
+ scanf ("%d" ,& n );
32
+ int a [n ],lis [n ];
33
+ for (i = 0 ;i < n ;i ++ ){
34
+ scanf ("%d" ,& a [i ]);
35
+ lis [i ]= 1 ;
36
+ }
37
+ node * head [n ];
38
+ for (i = 0 ;i < n ;i ++ ){
39
+ head [i ]= NULL ;
40
+ head [i ]= (node * )malloc (sizeof (node ));
41
+ head [i ]= append (head [i ],a [i ]);
42
+ }
43
+ for (i = 1 ;i < n ;i ++ ){
44
+ for (j = 0 ;j < i ;j ++ ){
45
+ if (a [i ]> a [j ] && lis [i ]< lis [j ]+ 1 ){
46
+ lis [i ]= lis [j ]+ 1 ;
47
+ head [i ]= append (head [j ],a [i ]);
48
+ }
49
+ }
50
+ }
51
+ int max = 0 ,index = 0 ;
52
+ for (i = 0 ;i < n ;i ++ ){
53
+ if (max < lis [i ]){
54
+ max = lis [i ];
55
+ index = i ;
56
+ }
57
+ }
58
+ // printf("%d %d\n",max,index);
59
+ printReverse (head [index ]);
60
+ printf ("\n" );
61
+ return 0 ;
62
+ }
You can’t perform that action at this time.
0 commit comments