Tutorialspoint
Problem
Solution
Submissions

Longest Increasing Subsequence

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

Write a C# 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 an 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 longest increasing subsequence is [2,3,7,101], therefore the length is 4.
Example 2
  • Input: nums = [0,1,0,3,2,3]
  • Output: 4
  • Explanation: The longest increasing subsequence is [0,1,2,3], therefore the length is 4.
Constraints
  • 1 ≤ nums.length ≤ 2500
  • -10^4 ≤ nums[i] ≤ 10^4
  • Time Complexity: O(n²)
  • Space Complexity: O(n)
ArraysDynamic Programming eBayOracle
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

  • Consider using dynamic programming to solve this problem
  • Create a DP array where dp[i] represents the length of the LIS ending at index i
  • For each element, compare with all previous elements to find the longest chain
  • The maximum value in the DP array will be your answer
  • For better time complexity, consider using binary search

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 values in dp array to 1 (minimum LIS length for any single element).
 Step 3: For each position i, compare with all previous positions j (where j < i).
 Step 4: If nums[i] > nums[j], update dp[i] = max(dp[i], dp[j] + 1).
 Step 5: Keep track of the maximum value in dp array during the process.
 Step 6: Return the maximum value as the longest increasing subsequence length.
 Step 7: For optimization, binary search can be used to achieve O(n log n) time complexity.

Submitted Code :