|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- 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 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?
复制代码
- class Solution:
- def lengthOfLIS(self, nums: List[int]) -> int:
- if nums == None or len(nums) == 0:
- return 0
- dp = [1 for _ in nums]
- res = 1
- for i in range(len(nums)):
- for j in range(i + 1, len(nums)):
- if nums[j] > nums[i]:
- dp[j] = max(dp[j], dp[i] + 1)
- res = max(res, dp[j])
- return res
复制代码 |
|