Professional Documents
Culture Documents
30-Longest Increasing Subsequence-22-03-2024
30-Longest Increasing Subsequence-22-03-2024
30-Longest Increasing Subsequence-22-03-2024
SUBSEQUENCE
TEST TIME ON MINIMUM STACK
URL:https://forms.gle/cqoa4QcjjL8rPkzD8
LONGEST INCREASING SUBSEQUENCE
EXPLANATION
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
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
2. Dynamic Programming
LONGEST INCREASING SUBSEQUENCE
APPROACH–RECURSIVE APPROACH
The recursive approach uses a function, `LIS`, to calculate the length of the
at index `i`.
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
incl = 0
Space Complexity:O(n) because the recursive call stack depth, at most, equals the length of
APPROACH–DYNAMIC PROGRAMMING
breaking it down into simpler overlapping subproblems, and their solutions are
complexity.
calculates the length of the LIS for each index by iteratively considering
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
Space Complexity:O(n) - Linear, due to the additional array used to store LIS values.
INTERVIEW QUESTIONS
sequence in which the elements are in strictly increasing order. The goal
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
O(n^2), where n is the length of the input array. This is due to the
4. Can you optimize the LIS algorithm for better time complexity?
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?
binary indexed tree, can significantly improve the efficiency of certain LIS
algorithms. These data structures enable faster queries and updates, which is
https://learn.codemithra.com