300. Longest Increasing Subsequence
# Medium(非常经典,一定要会)
This is type of sequence DP. 前i
个是否是LIS
. 这也叫区间动态规划,下角标从小到大计算,因为后面的元素基于前面的元素。
From index of 0
to n
, record longest increasing subsequence in a list. f[i]
means the longest increasing subsequence ending with nums[i]
.
Solution 1: time complex =
Define
f[i]
, records the longest increasing subsequence ending withnums[i]
Compute
f[i]
from index of0
,if nums[j] < nums[i] (j < i), f[i]=max(f[i], f[j]+1)
Loop
f[i]
to find biggest value, return it
Solution 2: time complex =
Save time when looking for the biggest element
nums[j]
less thannums[i]
by binary search.
Last updated
Was this helpful?