Problem
A subsequence of a permutation is a collection of elements of the permutation in the order that they appear. For example, (5, 3, 4) is a subsequence of (5, 1, 3, 4, 2).
A subsequence is increasing if the elements of the subsequence increase, and decreasing if the elements decrease. For example, given the permutation (8, 2, 1, 6, 5, 7, 4, 3, 9), an increasing subsequence is (2, 6, 7, 9), and a decreasing subsequence is (8, 6, 5, 4, 3). You may verify that these two subsequences are as long as possible.
Given: A positive integer followed by a permutation of length .
Return: A longest increasing subsequence of , followed by a longest decreasing subsequence of .
一子一的排列是按照它们出现的顺序排列的元素的集合。例如,(5、3、4)是(5、1、3、4、2)的子序列。
子序列被增加,如果该子序列增加的元件,并且降低如果元素减少。例如,给定排列(8,2,1,6,5,5,7,4,3,9),增加的子序列是(2,6,7,9,9),减少的子序列是(8,6, 5、4、3)。您可以验证这两个子序列尽可能长。
给定:一个正整数,后跟一个length的排列。
返回值:最长的递增子序列,然后是最长的递减子序列。
Sample Dataset
5
5 1 4 2 3
Sample Output
1 2 3
5 4 2
python解决方案:
def lengthOfLIS(nums):
"""
:type nums: List[int]
:rtype: int
"""
if nums == []:
return 0
N = len(nums)
Dp = [1] * N
for i in range(N - 1):
for j in range(0, i + 1):
if nums[i + 1] > nums[j]:
Dp[i + 1] = max(Dp[i + 1], Dp[j] + 1)
return Dp
nums = [5,1,4,2,3]
dp = lengthOfLIS(nums)
print(dp)