leetcode 之 Longest Increasing Subsequence

题目描述:

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?

即 给定一个无序的数组,找到最长的递增子序列。 返回其长度

解法1: 动态规划的解法。 设maxIns[i]  为以第i个数结尾的最长递增子序列的长度。

则maxIns[i+1]:

遍历0~i, 若nums[i+1] > nums[j] (j ~(0, i))  maxIns[i+1] = max(maxIns[i+1] , maxIns[i]+1)

算法的时间复杂度为o(n2), 空间复杂度为o(n), 代码如下:

 1 class Solution(object):
 2     def lengthOfLIS(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: int
 6         """
 7         ln = len(nums)
 8         if ln == 1 or ln == 0:
 9             return ln
10         lgst = [1]*(ln+1)
11         for i in range(1, ln):
12             for j in range(0, i):
13                 if nums[i] > nums[j]:
14                     lgst[i] = max(lgst[j]+1, lgst[i])
15         return max(lgst)
时间: 2024-10-13 06:10:52

leetcode 之 Longest Increasing Subsequence的相关文章

[LeetCode][JavaScript]Longest Increasing Subsequence

Longest Increasing Subsequence 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. Not

【Leetcode】Longest Increasing Subsequence

题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 题目: 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,

LeetCode 300. Longest Increasing Subsequence

300. Longest Increasing Subsequence Description Submission Solutions Add to List Total Accepted: 64115 Total Submissions: 170859 Difficulty: Medium Contributors: Admin Given an unsorted array of integers, find the length of longest increasing subsequ

[leetcode]300. Longest Increasing Subsequence最长递增子序列

Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be more

LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be

【LeetCode从零单刷】Longest Increasing Subsequence

题目: 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

LeetCode Number of Longest Increasing Subsequence

原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longe

[Leetcode] Binary search--300. Longest Increasing Subsequence

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 o

[LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] Outpu