albertonline· portal
blind75

Longest Increasing Subsequence: A Dynamic Programming Challenge

Dec 28, 2023 · Exploring the solution to find the length of the longest increasing subsequence in an array using dynamic programming.

The "Longest Increasing Subsequence" (LIS) problem is a classic example in the realm of dynamic programming. It involves identifying the length of the longest subsequence in a given array where the elements are in strictly increasing order.

Problem Statement

Given an integer array nums, the task is to find the length of the longest strictly increasing subsequence.

Examples

  • Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101].

  • Input: nums = [0,1,0,3,2,3] Output: 4 Explanation: One example of the longest increasing subsequence is [0,1,2,3].

JavaScript Solution (Dynamic Programming)

function lengthOfLIS(nums) {
  if (nums.length === 0) return 0;
  let dp = new Array(nums.length).fill(1);
  let maxAns = 1;

  for (let i = 1; i < nums.length; i++) {
    for (let j = 0; j < i; j++) {
      if (nums[i] > nums[j]) {
        dp[i] = Math.max(dp[i], dp[j] + 1);
      }
    }
    maxAns = Math.max(maxAns, dp[i]);
  }
  return maxAns;
}

Breaking Down the Solution

  • Initialize dp Array: A dynamic programming (dp) array is created and initially filled with 1s. This is because the minimum length of the Longest Increasing Subsequence (LIS) for each element is 1, considering each element as a subsequence by itself.

  • Iterate and Update dp: For each element in the array nums, the algorithm iterates and compares it with all previous elements. If nums[i] is greater than a previous element nums[j], the algorithm updates dp[i]. It sets dp[i] to be the maximum of its current value and dp[j] + 1. This update reflects the addition of the current element to the increasing subsequence ending at nums[j].

  • Track the Maximum Length: Throughout the iterations, the algorithm continually updates the maximum length of the LIS found so far. This is done by maintaining the maximum value in the dp array.

  • Return the LIS Length: The final result, which is the length of the longest increasing subsequence, is obtained from the maximum value in the dp array. This value represents the overall LIS in the entire array.

Conclusion

The Longest Increasing Subsequence problem is a key challenge in dynamic programming. It demonstrates the power and efficiency of this technique in solving complex computational problems. This problem is exemplary in illustrating the concepts of subsequences and showcases how dynamic programming can be used to build up solutions incrementally and efficiently, making it a staple in algorithmic problem-solving.

Comments (0)

Stub comments live in your browser only (localStorage). No server round-trip yet.

No comments yet. Be the first.