Tutorialspoint
Problem
Solution
Submissions

Longest Increasing Subsequence

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Write a JavaScript program to find the length of the longest strictly increasing subsequence in an array of integers. A subsequence is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements.

Example 1
  • Input: nums = [10,9,2,5,3,7,101,18]
  • Output: 4
  • Explanation:
    • The input array contains 8 elements with various values.
    • We need to find the longest subsequence where each element is strictly greater than the previous.
    • One possible longest increasing subsequence is [2,3,7,18].
    • Another valid subsequence of the same length is [2,5,7,101].
    • The length of the longest increasing subsequence is 4.
Example 2
  • Input: nums = [0,1,0,3,2,3]
  • Output: 4
  • Explanation:
    • The array has 6 elements with some repeated values.
    • We look for the longest sequence of strictly increasing elements.
    • The longest increasing subsequence is [0,1,2,3].
    • This subsequence maintains the relative order from the original array.
    • The maximum length achievable is 4.
Constraints
  • 1 <= nums.length <= 2500
  • -10^4 <= nums[i] <= 10^4
  • The subsequence must be strictly increasing
  • Elements must maintain their relative order from the original array
  • Time Complexity: O(n²) for basic approach, O(n log n) for optimized
  • Space Complexity: O(n)
ArraysDynamic Programming PwCPhillips
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use dynamic programming approach with an array to store the length of LIS ending at each position
  • For each element, check all previous elements and update the LIS length if current element is greater
  • Initialize dp array with 1s since each element forms a subsequence of length 1 by itself
  • For optimization, use binary search with a separate array to maintain the smallest tail elements
  • The answer is the maximum value in the dp array

Steps to solve by this approach:

  Step 1: Create a dynamic programming array (dp) where dp[i] represents the length of LIS ending at index i

 Step 2: Initialize all dp values to 1 since each individual element forms a subsequence of length 1
 Step 3: Use nested loops where the outer loop iterates through each position i from 1 to n-1
 Step 4: For each position i, check all previous positions j (0 to i-1) to find elements smaller than nums[i]
 Step 5: If nums[j] < nums[i], update dp[i] = max(dp[i], dp[j] + 1) to extend the subsequence
 Step 6: After processing all positions, find the maximum value in the dp array
 Step 7: Return the maximum dp value which represents the length of the longest increasing subsequence

Submitted Code :