0% found this document useful (0 votes)
7 views24 pages

Longest Increasing Subsequence

The document explains the concept of Longest Increasing Subsequence (LIS), providing definitions, examples, and algorithms for solving the problem using recursive and dynamic programming approaches. It details the time and space complexities associated with these methods and discusses interview questions related to LIS, including optimization techniques. The document concludes with contact information for further inquiries.

Uploaded by

jayasruthyk6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views24 pages

Longest Increasing Subsequence

The document explains the concept of Longest Increasing Subsequence (LIS), providing definitions, examples, and algorithms for solving the problem using recursive and dynamic programming approaches. It details the time and space complexities associated with these methods and discusses interview questions related to LIS, including optimization techniques. The document concludes with contact information for further inquiries.

Uploaded by

jayasruthyk6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

LONGEST INCREASING

SUBSEQUENCE
TEST TIME ON MINIMUM STACK

URL:https://forms.gle/cqoa4QcjjL8rPkzD8
LONGEST INCREASING SUBSEQUENCE
EXPLANATION

A Longest Increasing Subsequence (LIS) is a


subsequence of a given sequence of numbers (not
necessarily contiguous) in which the elements are in
strictly increasing order.
In other words, the Longest Increasing Subsequence
problem asks for the length of the longest subsequence
such that all elements of the subsequence are sorted
in ascending order.
LONGEST PALINDROMIC SUBSEQUENCE
EXAMPLE

Consider the input sequence: [10, 22, 9, 33, 21, 50, 41, 60, 80]

The Longest Increasing Subsequence in this case is: [10, 22, 33,
50, 60, 80]
LONGEST INCREASING SUBSEQUENCE
EXPLANATION

We start with the first element, 10, and consider it


as the first element of a potential increasing
subsequence.
Move to the next element, 22. It's greater than 10, so
we include it in the potential subsequence.
Move to the next element, 9. It's less than 22, so we
can't include it in the current subsequence. We skip
it.
Move to the next element, 33. It's greater than 22, so
we include it in the potential subsequence.
LONGEST INCREASING SUBSEQUENCE
EXPLANATION

Move to the next element, 21. It's less than 33, so we


skip it.
Move to the next element, 50. It's greater than 33, so
we include it in the potential subsequence.
Move to the next element, 41. It's less than 50, so we
skip it.
Move to the next element, 60. It's greater than 50, so
we include it in the potential subsequence.
Move to the next element, 80. It's greater than 60, so
we include it in the potential subsequence.
The final Longest Increasing Subsequence is [10, 22,
33, 50, 60, 80] with a length of 6.
LONGEST INCREASING SUBSEQUENCE
APPROACH

1. The recursive approach


2. Dynamic Programming
LONGEST INCREASING SUBSEQUENCE
RECURSIVE APPROACH

⮚ The recursive approach uses a function, `LIS`, to


calculate the length of the Longest Increasing
Subsequence by considering or excluding the current
element at index `i`.
⮚ It explores both options recursively, returning the
maximum length.
⮚ The base case terminates the recursion when the
index `i` reaches the length of the array.
LONGEST INCREASING SUBSEQUENCE
APPROACH

function LIS(arr, i, n, prev):


if i equals n:
return 0
excl = LIS(arr, i + 1, n, prev)
incl = 0
if arr[i] is greater than prev:
incl = 1 + LIS(arr, i + 1, n, arr[i])
return maximum of incl and excl
LONGEST INCREASING SUBSEQUENCE
class Main return Integer.max(incl,
{ excl);
public static int }
LIS(int[] arr, int i, int n,
int prev) public static void
{ main(String[] args)
if (i == n) { {
return 0; int[] arr = { 0, 8, 4,
} 12, 2, 10, 6, 14, 1, 9, 5, 13,
int excl = LIS(arr, i 3, 11, 7, 15 };
+ 1, n, prev);
int incl = 0;
if (arr[i] > prev) { System.out.print(LIS(arr, 0,
incl = 1 + arr.length,
LIS(arr, i + 1, n, arr[i]); Integer.MIN_VALUE));
} }
}
LONGEST INCREASING SUBSEQUENCE
import java.util.*; {
public class Main { return 0;
public static void main(String[] args) { }
Scanner sc = new Scanner(System.in); int n = sequence.length;
int n=sc.nextInt(); int[] dp = new int[n];
int sequence[]=new int[n]; Arrays.fill(dp, 1);
for(int i=0;i<n;i++){ for (int i = 1; i < n; i++) {
sequence[i]=sc.nextInt(); for (int j = 0; j < i; j++) {
} if (sequence[i] >
int sequence[j]) {
longestIncreasingSubsequenceLength = dp[i] = Math.max(dp[i], dp[j] + 1);
findLongestIncreasingSubsequence(sequence); }
}
System.out.println(longestIncreasingSubsequen }
ceLength); int max = 0;
} for (int length : dp) {
public static int max = Math.max(max, length);
findLongestIncreasingSubsequence(int[] }
sequence) {
if (sequence == null || sequence.length == 0) return max;
{ }
return 0; }
}
int n = sequence.length;
int[] dp = new int[n];
LONGEST INCREASING SUBSEQUENCE
TIME AND SPACE COMPLEXITY

Time Complexity: O(2^n) due to two recursive calls per


element.

Space Complexity:O(n) because the recursive call stack


depth, at most, equals the length of the input array.
LONGEST INCREASING SUBSEQUENCE
APPROACH-DYNAMIC PROGRAMMING

⮚ Dynamic Programming is a technique where a complex


problem is solved by breaking it down into simpler
overlapping subproblems, and their solutions are stored
to avoid redundant computations, leading to optimized
time and space complexity.
⮚ In the context of the Longest Increasing Subsequence
code, it efficiently calculates the length of the LIS
for each index by iteratively considering previous
indices and storing optimal solutions.
LONGEST INCREASING SUBSEQUENCE
APPROACH

Function lis(arr, n):


lis = new Array of size n, initialized with 1
for i from 1 to n-1:
for j from 0 to i-1:
if arr[i] > arr[j] and lis[i] < lis[j] + 1:
lis[i] = lis[j] + 1
max = 0
for i from 0 to n-1:
if max < lis[i]:
max = lis[i]
LONGEST INCREASING SUBSEQUENCE

class Main { max = lis[i];


static int lis(int arr[], int return max;
n) }
{ public static void main(String
int lis[] = new args[])
int[n]; {
int i, j, max = 0; int arr[] = { 10, 22, 9, 33, 21, 50,
for (i = 0; i < n; i+ 41, 60 };
+) int n = arr.length;
lis[i] = 1; System.out.println(lis(arr,
for (i = 1; i < n; i+ n));
+) }
for (j = 0; j < i; j+ }
+)
if (arr[i] > arr[j] && lis[i] < lis[j]
+ 1)
lis[i] = lis[j] + 1;
for (i = 0; i < n; i+
+)
LONGEST INCREASING SUBSEQUENCE
TIME AND SPACE COMPLEXITY

Time Complexity:O(n^2) - Quadratic, where n is the length


of the input array.

Space Complexity:O(n) - Linear, due to the additional


array used to store LIS values.
INTERVIEW QUESTIONS

1. Explain the concept of Longest Increasing Subsequence.

Answer: The Longest Increasing Subsequence is a subsequence


of a given sequence in which the elements are in strictly
increasing order. The goal is to find the length of the
longest such subsequence.
INTERVIEW QUESTIONS

2. Describe an algorithm to solve the Longest Increasing


Subsequence problem.
Answer:One common algorithm is the dynamic programming approach.
Initialize an array to store LIS values for each index. Iterate
through the array, comparing each element with the previous ones
to update LIS values. The final answer is the maximum value in
the LIS array.
INTERVIEW QUESTIONS

3. What is the time complexity of the dynamic programming solution


for LIS?

Answer:The time complexity of the dynamic programming


solution for LIS is O(n^2), where n is the length of the
input array. This is due to the nested loop structure in
the algorithm.
INTERVIEW QUESTIONS

4. Can you optimize the LIS algorithm for better time


complexity?
Answer: Yes, an optimized solution using Patience Sorting
or a binary search based approach can achieve O(n log n)
time complexity. These approaches exploit the properties of
the LIS problem to reduce the overall computational cost.C
INTERVIEW QUESTIONS

5. How does the choice of data structure impact the efficiency


of solving LIS?
Answer: Choosing an appropriate data structure, such as a
segment tree or a binary indexed tree, can significantly
improve the efficiency of certain LIS algorithms. These
data structures enable faster queries and updates, which is
beneficial in more advanced LISsolving techniques.
https://learn.codemithra.com
THANK
YOU

+91 78150 [email protected] www.codemithra.com


95095

You might also like