Sponsored
Sponsored
This approach uses dynamic programming to solve the problem in O(n^2) time complexity. We maintain a dp array where dp[i] represents the length of the longest increasing subsequence that ends with nums[i]. For each element, we iterate over all previous elements to see if they can be included in the subsequence ending at the current element, and update dp[i] accordingly.
Time Complexity: O(n^2) where n is the length of the input array. Space Complexity: O(n) for storing the dp array.
1function lengthOfLIS(nums) {
2 if (nums.length === 0) return 0;
3 const dp = Array(nums.length).fill(1);
4 let maxLen = 1;
5 for (let i = 1; i < nums.length; i++) {
6 for (let j = 0; j < i; j++) {
7 if (nums[i] > nums[j]) {
8 dp[i] = Math.max(dp[i], dp[j] + 1);
9 }
10 }
11 maxLen = Math.max(maxLen, dp[i]);
12 }
13 return maxLen;
14}
15
16console.log(lengthOfLIS([10,9,2,5,3,7,101,18]));In JavaScript, we use similar logic as other languages to iteratively build the dp array. The max length is calculated and updated through nested conditions.
In this approach, we use a combination of dynamic programming and binary search to solve the problem in O(n log n) time. This is often called the 'patience sorting' technique where we maintain a list, 'ends', to store the smallest ending value of any increasing subsequence with length i+1 in 'ends[i]'. We use binary search to find the position where each element in nums can be placed in 'ends'.
Time Complexity: O(n log n). Space Complexity: O(n).
Python solution makes use of the bisect module to apply binary search on the list 'ends'. This approach ensures finding the right position for each element in logarithmic time.