Given a sequence of numbers in the form of a linked list lis. Find the length of the Longest Increasing Subsequence(LIS) of the given Linked List.
Examples:
Input: list = 3->10->2->1->20
Output: 3
Explanation: The longest increasing subsequence is 3->10-> 20
Input: list = 3-> 2
Output: 1
Explanation: The longest increasing subsequence are 3 and 2
Input: list = 50->3->10->7->40->80
Output: Length of LIS = 4
Explanation: The longest increasing subsequence is {3->7->40->80} or {3->10->40->80}
Approach: The basic intuition of the solution is to start iterating from the first node to the end of linked list .In the process of moving calculate length of LIS ending at every node and store it in a count variable. Finally, calculate maximum count value among all nodes. Follow the steps mentioned below to solve the problem:
- Traverse the linked list from the starting node.
- LIS length of a linked list with one node is 1 .So we initialize every node count variable to 1.
- For every ith node traverse the first (i-1) nodes and do the following:
- if value of current node is greater than the value of previous node, extend sequence length.
- As maximum length ending with current node is required. select node from first (i-1) nodes which satisfy the previous condition and have maximum count value .
- Once all the nodes are traversed following the above procedure .find maximum count value among all nodes.
- The maximum count value is the required length of the LIS.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
struct Node* next;
int count;
};
int LIS( struct Node* head)
{
if (head == NULL)
return 0;
if (head->next == NULL)
return 1;
Node* curr_p = head->next;
while (curr_p != NULL) {
int maxi = 0;
Node* prev_p = head;
while (prev_p != curr_p) {
if (curr_p->data
> prev_p->data) {
if (prev_p->count > maxi) {
maxi = prev_p->count;
}
}
prev_p = prev_p->next;
}
curr_p->count = 1 + maxi;
curr_p = curr_p->next;
}
int LIS_length = 0;
curr_p = head;
while (curr_p != NULL) {
if (LIS_length < curr_p->count) {
LIS_length = curr_p->count;
}
curr_p = curr_p->next;
}
return LIS_length;
}
void push( struct Node** head_ref,
int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->count = 1;
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;
push(&head, 20);
push(&head, 1);
push(&head, 2);
push(&head, 10);
push(&head, 3);
int ans = LIS(head);
cout << ans;
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node {
int data;
Node next;
int count;
};
static int LIS(Node head)
{
if (head == null )
return 0 ;
if (head.next == null )
return 1 ;
Node curr_p = head.next;
while (curr_p != null ) {
int maxi = 0 ;
Node prev_p = head;
while (prev_p != curr_p) {
if (curr_p.data
> prev_p.data) {
if (prev_p.count > maxi) {
maxi = prev_p.count;
}
}
prev_p = prev_p.next;
}
curr_p.count = 1 + maxi;
curr_p = curr_p.next;
}
int LIS_length = 0 ;
curr_p = head;
while (curr_p != null ) {
if (LIS_length < curr_p.count) {
LIS_length = curr_p.count;
}
curr_p = curr_p.next;
}
return LIS_length;
}
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
new_node.count = 1 ;
head_ref = new_node;
return head_ref;
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 20 );
head = push(head, 1 );
head = push(head, 2 );
head = push(head, 10 );
head = push(head, 3 );
int ans = LIS(head);
System.out.print(ans);
}
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self . next = None
self .count = 1
def LIS(head):
if (head = = None ):
return 0
if (head. next = = None ):
return 1
curr_p = head. next
while (curr_p ! = None ):
maxi = 0
prev_p = head
while (prev_p ! = curr_p):
if (curr_p.data > prev_p.data):
if (prev_p.count > maxi):
maxi = prev_p.count
prev_p = prev_p. next
curr_p.count = 1 + maxi
curr_p = curr_p. next
LIS_length = 0
curr_p = head
while (curr_p ! = None ):
if (LIS_length < curr_p.count):
LIS_length = curr_p.count
curr_p = curr_p. next
return LIS_length
head = Node( 3 )
head. next = Node( 10 )
head. next . next = Node( 2 )
head. next . next . next = Node( 1 )
head. next . next . next . next = Node( 20 )
ans = LIS(head)
print (ans)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public class Node {
public int data;
public Node next;
public int count;
};
static int LIS(Node head)
{
if (head == null )
return 0;
if (head.next == null )
return 1;
Node curr_p = head.next;
while (curr_p != null ) {
int maxi = 0;
Node prev_p = head;
while (prev_p != curr_p) {
if (curr_p.data > prev_p.data) {
if (prev_p.count > maxi) {
maxi = prev_p.count;
}
}
prev_p = prev_p.next;
}
curr_p.count = 1 + maxi;
curr_p = curr_p.next;
}
int LIS_length = 0;
curr_p = head;
while (curr_p != null ) {
if (LIS_length < curr_p.count) {
LIS_length = curr_p.count;
}
curr_p = curr_p.next;
}
return LIS_length;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
new_node.count = 1;
head_ref = new_node;
return head_ref;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 20);
head = push(head, 1);
head = push(head, 2);
head = push(head, 10);
head = push(head, 3);
int ans = LIS(head);
Console.Write(ans);
}
}
|
Javascript
<script>
class Node {
constructor(d) {
this .data = d;
this .next = null ;
this .count = 1;
}
};
function LIS(head)
{
if (head == null )
return 0;
if (head.next == null )
return 1;
let curr_p = head.next;
while (curr_p != null ) {
let maxi = 0;
let prev_p = head;
while (prev_p != curr_p) {
if (curr_p.data
> prev_p.data) {
if (prev_p.count > maxi) {
maxi = prev_p.count;
}
}
prev_p = prev_p.next;
}
curr_p.count = 1 + maxi;
curr_p = curr_p.next;
}
let LIS_length = 0;
curr_p = head;
while (curr_p != null ) {
if (LIS_length < curr_p.count) {
LIS_length = curr_p.count;
}
curr_p = curr_p.next;
}
return LIS_length;
}
let head = new Node(3);
head.next = new Node(10);
head.next.next = new Node(2);
head.next.next.next = new Node(1);
head.next.next.next.next = new Node(20);
let ans = LIS(head);
document.write(ans);
</script>
|
Time Complexity: O(N * N) where N is the length of the linked list
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Feb, 2022
Like Article
Save Article