Longest Increasing Subsequence

'N' students are standing in a row. You are given the height of every student standing in the row. Your task is to find the longest strictly increasing subsequence of heights from the row such that the relative order of the students does not change.

A subsequence is a sequence that can be derived from another sequence by deleting zero or more elements without changing the order of the remaining elements.

Input format:

The first line of input contains an integer ‘T’ denoting the number of test cases.

The first line of each test case contains an integer ‘N’ , the number of students in the row. 

The second line of each test case contains ‘N’ space separated integers representing the height of every student in the row. 

Output format:

For each test case, return the length of longest strictly increasing subsequence of heights.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 200
1 <= heights[i] <=10^9

Time Limit: 1 sec
CodingNinjas
author
2y

This is a variation of the standard LIS and LCS problem.

CodingNinjas
author
2y
Brute force approach

The idea is to find strictly increasing subsequences for all the elements of the array once including the elements in the sequence and once excluding it. In order to do this, we us...read more

CodingNinjas
author
2y
Memoization approach

LIS => Longest Increasing Subsequence.

Here, LIS(4) corresponds to LIS found up to index 4. LIS(4) will be calculated with the help of LIS(3), LIS(2), LIS(1).

Now, LIS(3) is calcula...read more

CodingNinjas
author
2y
Dynamic Programming

This approach relies on the fact that the LIS possible upto ith index is independent of the elements coming after the ith index. Thus, for every index we will store the length of LI...read more

Add answer anonymously...
SAP Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter