LC300 - Longest Increasing Subsequence
Problem
Given an integer array nums
, return the length of the longest strictly increasing subsequence. Note that the subsequence does not need to be contiguous.
Example
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.
Solution
DFS
Use DFS to generate all possible subsequences by having a decision tree, where each array index is a node, and the leaves are the indices that can be added to the subsequence. However this is really expensive due to the deep LIS call stack, which gives a time complexity of .
Cached DFS
Since there are repeated calls to find the LIS of the same index/subtree, we can use the @cache
decorator to reduce compute. Time complexity of this approach is , because repeated calls to LIS with the same argument returns cached results. We use extra space for the cache.
Last updated