300. Longest Increasing Subsequence¶
Edited by Charles
Medium · Python · Dynamic Programming
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Given an integer array nums, find the length of the Longest Increasing Subsequence (LIS).
Note that a subsequence does not need to be contiguous. It only needs to preserve the relative order of the elements.
Original study notes follow, including intermediate snippets and earlier attempts.
Idea¶
Given an integer array nums, find the length of the Longest Increasing Subsequence (LIS).
Note that a subsequence does not need to be contiguous. It only needs to preserve the relative order of the elements.
For example:
One possible longest increasing subsequence is:
Therefore, the answer is 4.
Dynamic Programming¶
State Definition¶
Let
denote:
The length of the longest increasing subsequence ending at
nums[i].
The phrase "ending at nums[i]" is very important.
For example:
Then
Initialization¶
Each element itself forms an increasing subsequence of length 1.
Therefore,
That is,
State Transition¶
For each position i, iterate through all previous positions j.
If
then nums[i] can be appended to the increasing subsequence ending at nums[j].
Update:
Transition equation:
The idea is:
Find all previous elements that can precede
nums[i], choose the longest subsequence among them, and appendnums[i].
Why Can't We Return dp[-1]?¶
The longest increasing subsequence does not necessarily end at the last element.
For example,
Then
The LIS is
with length 3, while
Therefore, the final answer should be
or maintain a variable res to keep track of the global maximum during iteration.
Example¶
Initialization:
i = 1, nums[1] = 10¶
Can be appended after:
Update:
i = 2, nums[2] = 4¶
Previous elements:
No update:
i = 3, nums[3] = 3¶
There is no previous element smaller than 3.
i = 4, nums[4] = 8¶
Possible subsequences:
Update:
i = 5, nums[5] = 9¶
Can be appended after:
Update:
Final answer:
Complexity Analysis¶
Time Complexity
For each element, we iterate through all previous elements.
Space Complexity
A DP array of length n is required.
Summary¶
State Definition
Initialization
State Transition
Traversal Order
Traverse the array from left to right. For each position, examine all previous elements.
Final Answer
Common Mistakes¶
dp[i]represents the LIS ending atnums[i], not the LIS within the firstielements.- Do not return
dp[-1], since the LIS does not necessarily end at the last element. - The sequence must be strictly increasing, so the condition must be
instead of
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | August 9, 2026 |
Python solution¶
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
l = len(nums)
if l == 1:
return 1
dp = [1 for _ in range(l)]
res = 1
for i in range(1, l):
for j in range(i):
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j] + 1)
res = max(res, dp[i])
return res