1、动态规划问题导览:
- 最长上升子序列(longest increasing subsequence)问题,也可以叫最长非降序子序列,简称LIS。是动态规划算法的一个经典应用。
- 我们都知道,动态规划的一个特点就是当前解可以由上一个阶段的解推出, 由此,把我们要求的问题简化成一个更小的子问题。子问题具有相同的求解方式,只不过是规模小了而已。最长上升子序列就符合这一特性。我们要求n个数的最长上升子序列,可以求前n-1个数的最长上升子序列,再跟第n个数进行判断。求前n-1个数的最长上升子序列,可以通过求前n-2个数的最长上升子序列……直到求前1个数的最长上升子序列,此时LIS当然为1。
2、常见面试题:
①题目描述:
给定一组整数序列,求最长升序子序列的长度。
如序列(1,7,3,5,9,4,8)中包含(1,7),(3,4,8)等多个升序子序列,其中最长的是(1,3,5,8),因此最长升序子序列长度为4.
输入描述:
输入为2行,第一行是一个正整数n(1<=n<=10000),表示序列长度,第二行包含n个int类型证书的序列,空格分隔。
输出描述:
输出最长升序子序列的长度
②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 is4. 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(nlogn) time complexity?
Credits:
Special thanks to@pbrotherfor adding this problem and creating all test cases.
Subscribeto see which companies asked this question.
翻译