Longest Increasing Subsequence – Python 3 (Week 17 – 09)

class Solution:
    """
    @param nums: An integer array
    @return: The length of LIS (longest increasing subsequence)
    """
    def longestIncreasingSubsequence(self, nums):
        # write your code here
        if nums is None or not nums:
            return 0
        
        dp = [1] * len(nums)
        for curr, val in enumerate(nums):
            for prev in range(curr):
                if nums[prev] < val:
                    dp[curr] = max(dp[curr], dp[prev] + 1)
                    
        return max(dp)

Time O(n^2). Space O(n).

Leave a comment

Design a site like this with WordPress.com
Get started