Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Longest Increasing Subsequence. Blind 75. Dynamic Programming.
Aug 22, 2024
325 views
Written by Array Alchemist
LeetCode wizard with 75 conquered challenges under my belt.
Turning algorithm nightmares into "Aha!" moments, one explanation at a time.
Join me to level up your coding skills – from interviews to real-world application!
Given an integer array nums, return the length of the longest strictly increasing
subsequence
Example 1:
Example 2:
Example 3:
Let's first conclude what we can from the question:
What are some application of longest-increasing-subsequence in the Real world?
Historical data: Which sequences of movies have other similar users enjoyed most?
comparing left element to the right element. if(input[left] < input[right]){ // we update the value of currentComaprision position of longestIncreasingSubsequence // we update the max }To find the length of the LIS, we need to consider the previous elements in the array and their contributions to the increasing subsequence
how do we know this? we can have an array, named dp that will give us the contribution to the increasing subsequence
if at any point,
else
if @ any point left meets right
var lengthOfLIS = function(nums) { const dp = new Array(nums.length).fill(1) let max = 1 for(let left = 0; left < nums.length; left++){ for(let right = left + 1; right < nums.length; right++ ){ if(nums[left] < nums[right]){ dp[right] = Math.max(dp[right] , dp[left] + 1) max = Math.max(max, dp[right]) } } } return max };Submitting to the leetcode:
Complexities:
Space Complexity: O(N), N is the length of the array
Time Complexity: O(N^2),