LeetCode 300 Longest Increasing Subsequence - Implementation

The approach

For each position, find best previous ending to extend.

def lengthOfLIS(nums): n = len(nums) dp = [1] * n

for i in range(1, n):
    for j in range(i):
        if nums[j] < nums[i]:
            dp[i] = max(dp[i], dp[j] + 1)

return max(dp)

O(n2)O(n^2) time, O(n)O(n) space.