
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.
- The input array contains 8 elements with various values.
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.
- The array has 6 elements with some repeated values.
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)
Editorial
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. |
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