Advertisement

Longest Increasing Subsequence - LeetCode 300 Solution

Longest Increasing Subsequence - Complete Solution Guide

Longest Increasing Subsequence is LeetCode problem 300, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer array nums , return the length of the longest strictly increasing subsequence . 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 Example 3: Input: nums = [7,7,7,7,7,7,7] Output: 1 Constraints: 1 <= nums.length <= 2500 -10 4 <= nums[i] <= 10 4 Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity?

Detailed Explanation

The problem asks us to find the length of the longest strictly increasing subsequence within a given 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. 'Strictly increasing' means that each element in the subsequence must be greater than the previous one. The input is an array of integers, and the output is an integer representing the length of the longest increasing subsequence.

Solution Approach

The solution uses a dynamic programming strategy optimized with binary search. It maintains an array called `tails`, where `tails[i]` represents the smallest tail of all increasing subsequences of length `i+1`. The algorithm iterates through the input array `nums`. For each number, it either extends the current longest increasing subsequence (if the number is larger than all tails) or finds the appropriate position in the `tails` array to update (using binary search). The length of the `tails` array at the end is the length of the longest increasing subsequence.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty list `tails` to store the smallest tail of all increasing subsequences of different lengths.
  2. Step 2: Iterate through each `num` in the input array `nums`.
  3. Step 3: Perform a binary search on the `tails` array to find the smallest tail that is greater than or equal to `num`.
  4. Step 4: If the binary search returns an index equal to the length of `tails`, it means `num` is greater than all tails in the `tails` array. In this case, append `num` to the `tails` array, extending the longest increasing subsequence.
  5. Step 5: If the binary search returns an index `idx` less than the length of `tails`, it means `num` is smaller than or equal to `tails[idx]`. Replace `tails[idx]` with `num`. This step does not change the length of the longest increasing subsequence found so far, but it potentially allows us to build a longer subsequence later on because we are replacing the existing tail with a smaller number.
  6. Step 6: After processing all numbers in `nums`, the length of the `tails` array is the length of the longest increasing subsequence. Return the length of `tails`.

Key Insights

  • Insight 1: We can use dynamic programming or a binary search based approach to solve this problem efficiently. The binary search approach is more efficient in terms of time complexity (O(n log n)).
  • Insight 2: The core idea of the binary search approach is to maintain a 'tails' array. `tails[i]` is the smallest tail of all increasing subsequences with length `i+1`.
  • Insight 3: If we encounter a number `num` that is greater than all tails in the `tails` array, it means we can extend the longest increasing subsequence by one. If `num` is smaller than some tail, we can potentially replace that tail with `num` to create a smaller tail, potentially enabling a longer increasing subsequence later on.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Binary Search, Dynamic Programming.

Companies

Asked at: Accenture, Atlassian, Autodesk, Citadel, Commvault, Druva, Expedia, Flexport, Goldman Sachs, IBM, Intuit, Licious, PayPal, Pure Storage, Samsung, Splunk, Wayfair, Yandex, Zoho.