leetcode dynamic programming

简介: 300. Longest Increasing SubsequenceQuestionGiven an unsorted array of integers, find the length of longest increasing subsequence.

300. Longest Increasing Subsequence

Question

Given an unsorted array of integers, find the length of longest increasing subsequence.

For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.

Your algorithm should run in O(n2) complexity.

Follow up: Could you improve it to O(n log n) time complexity?

Solution

dp[i] means the length of an increase sequence that includes nums[i], and it must equals a previous dp value add one which reaches the max.

class Solution(object):
    def lengthOfLIS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        dp = [1]*len(nums)
        for i in xrange(len(nums)):
            for y in xrange(i):
                if nums[i] >nums[j]:
                    dp[i] = max(dp[i],dp[j]+1)
        return max(dp) if dp else 0
目录
相关文章
|
6月前
|
算法 安全 编译器
【C++20 新特性Concepts 概念】C++20 Concepts: Unleashing the Power of Template Programming
【C++20 新特性Concepts 概念】C++20 Concepts: Unleashing the Power of Template Programming
273 0
|
5月前
|
存储 算法 Java
动态规划详解(Dynamic Programming)
动态规划详解(Dynamic Programming)
44 1
|
6月前
动态规划(Dynamic Programming)详解
动态规划(Dynamic Programming)详解
34 0
|
6月前
|
机器人
动态规划Dynamic Programming
动态规划Dynamic Programming
43 0
|
6月前
|
机器学习/深度学习 存储 算法
算法·动态规划Dynamic Programming
算法·动态规划Dynamic Programming
37 0
|
存储 算法 Python
Dynamic Programming,简称 DP
动态规划(Dynamic Programming,简称 DP)是一种在数学、计算机科学和经济学中使用的,通过把原问题分解为相对简单的子问题的方式求解复杂问题的方法。动态规划的核心思想是,将问题分解成若干个子问题,通过求解子问题并将子问题的解存储起来,以便在需要时可以重复使用,从而避免了重复计算,提高了算法的效率
98 3
|
机器学习/深度学习 算法
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
209 0
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
|
存储 算法 安全
动态规划(Dynamic Programming)
DP是解决多阶段决策过程中最优化问题的一种常用方法,它在算法中的重要性不言而喻,本文将帮助大家简单了解DP。
156 0
动态规划(Dynamic Programming)
|
SQL 移动开发 算法
New Dynamic Programming Algorithm for the Generation of Optimal Bushy Join Trees
MySQL无疑是现在开源关系型数据库系统的霸主,在DBEngine的最新排名中仍然稳居第2位,与第3位SQL Server的积分差距并不算小,可以说是最受欢迎,使用度最高的数据库系统,这一点看看有多少新型数据库要兼容MySQL的协议和语法就知道了。
323 0
New Dynamic Programming Algorithm for the Generation of Optimal Bushy Join Trees
|
JavaScript Java Go
Rise of Kotlin: The Programming Language for the Next Generation
Rise of Kotlin: The Programming Language for the Next Generation https://hackernoon.
1562 0