Length of longest increasing subsequence-Coding question asked by LinkedIn, Google, Goldmann Sachs

Problem Statement:

Given an integer array nums, return the length of the longest strictly increasing subsequence.

A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements.

For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].

Input Format:

Output Format:

Sample Input 1:

Sample Output 1:

Sample input 2:

Sample output 2:

Sample Input 3:

Sample Output 3:

Constraint:

  • 1 <= nums.length <= 2500
  • -104 <= nums[i] <= 10⁴

Approach:

To find the LIS for a given sequence nums, we need to return max(nums(i)).
The length of the longest increasing subsequence ending at index i, will be 1 greater than the maximum of lengths of all longest increasing subsequences ending at indices before i. Therefore we can take a sequence of length of nums and solve iteratively where the element at index i be increased by 1 in each iteration.

Code:

Thanks for Reading

Placewit grows the best engineers by providing an interactive classroom experience and by helping them develop their skills and get placed in amazing companies.

Learn more at Placewit. Follow us on Instagram and Facebook for daily learning.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store