Learn Code
Depth-First Search
Greedy Algorithms
Dynamic Programming
Longest Increasing Subsequence
medium
DESCRIPTION (credit Leetcode.com)
Given an integer array nums, write a function that returns the length of the longest increasing subsequence within the array. The subsequence does not have to be contiguous, but the elements must be strictly increasing (i.e. nums[i] < nums[j] for all i < j).
Input:
Output:
Explanation: The longest increasing subsequence is [2,4,6,12], which has a length of 4.
Explanation
longest increasing subsequence
0 / 1
i = 1
0 / 5
Solution
longest increasing subsequence
0 / 32
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n²) where n
is the length of the input array nums
. We use two nested for-loops to iterate over all elements in nums
.
Space Complexity: O(n) where n
is the length of the input array nums
. We use an additional array dp
of length n
to store the length of the longest increasing subsequence ending at each index.
Login to track your progress
Your account is free and you can post anonymously if you choose.