30-Longest Increasing Subsequence-22-03-2024

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

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 INCREASING 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

APPROACH–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, 3, 11, 7, 15 };
int excl = LIS(arr, i + 1, n,
prev); System.out.print(LIS(arr, 0,
int incl = 0; arr.length, Integer.MIN_VALUE));
if (arr[i] > prev) { }
incl = 1 + LIS(arr, i + 1, n, }
arr[i]);
}
LONGEST INCREASING SUBSEQUENCE

import java.util.*; Arrays.fill(dp, 1);


public class Main { for (int i = 1; i < n; i++) {
public static void main(String[] args) { for (int j = 0; j < i; j++) {
Scanner sc = new Scanner(System.in); if (sequence[i] > sequence[j]) {
int n=sc.nextInt(); dp[i] = Math.max(dp[i], dp[j] + 1);
}
int sequence[]=new int[n];
}
for(int i=0;i<n;i++){
}
sequence[i]=sc.nextInt(); int max = 0;
} for (int length : dp) {
int longestIncreasingSubsequenceLength = max = Math.max(max, length);
findLongestIncreasingSubsequence(sequence); }

System.out.println(longestIncreasingSubsequenceLengt return max;


h); }
} }
public static int
findLongestIncreasingSubsequence(int[] sequence) {
if (sequence == null || sequence.length == 0) {
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]
return max
LONGEST INCREASING SUBSEQUENCE

class Main { max = lis[i];


static int lis(int arr[], int n) return max;
{ }
int lis[] = new int[n]; public static void main(String args[])
int i, j, max = 0; {
for (i = 0; i < n; i++) int arr[] = { 10, 22, 9, 33, 21, 50, 41,
lis[i] = 1; 60 };
for (i = 1; i < n; i++) int n = arr.length;
for (j = 0; j < i; j++) System.out.println(lis(arr, n));
if (arr[i] > arr[j] && lis[i] < lis[j] + 1) }
lis[i] = lis[j] + 1; }
for (i = 0; i < n; i++)
if (max < lis[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

searchbased approach can achieve O(n log n) time complexity. These

approaches exploit the properties of the LIS problem to reduce the overall

computational cost.
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.


/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra

https://learn.codemithra.com

codemithra@ethnus.com +91 7815 095 095 +91 9019 921 340

You might also like