|
| 1 | +## Longest Increasing Subsequence |
| 2 | + |
| 3 | +### 描述 |
| 4 | + |
| 5 | +Given an unsorted array of integers, find the length of longest increasing subsequence. |
| 6 | + |
| 7 | +For example, |
| 8 | + |
| 9 | +Given `[10, 9, 2, 5, 3, 7, 101, 18]`, |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +Your algorithm should run in `O(n^2)` complexity. |
| 14 | + |
| 15 | +**Follow up**: Could you improve it to O(n log n) time complexity? |
| 16 | + |
| 17 | +### 解法1 动规 |
| 18 | + |
| 19 | +这是一个多阶段决策问题,求最长,是一个最优化问题,用 BFS, 贪心或DP。 |
| 20 | + |
| 21 | +如果用BFS,首先用数组中的所有元素作为根节点,形成n颗树,每棵树开始往下扩展,出现逆序则终止,最后计算每棵树的高度,取最大,就是最终结果。算法复杂度为`O(n!)`。 |
| 22 | + |
| 23 | +本题中,一个节点往下扩展的时候,没有一个确定的准则,让你走哪些边,本题不具有贪心选择性质,因此不能用贪心法。 |
| 24 | + |
| 25 | +我们来尝试用DP来解决这题。最重要的是要定义出状态。首先从状态扩展这方面看,对于数组中的一个元素,它往后走,凡是比它大的元素,都可以作为下一步,因此这里找不到突破口。 |
| 26 | + |
| 27 | +我们换一个角度,从结果来入手,我们要求的最长递增子序列,一个递增子序列,肯定是有首尾两个端点的,假设我们定义 f[i] 为以第`i`个元素为起点的最长递增子序列,那么 `f[i]`和`f[j]`之间没有必然联系,这个状态不好用。 |
| 28 | + |
| 29 | +假设定义`f[i]`为以第`i`个元素为终点的最长递增子序列,那么如果`i<j`且`nums[i]<nums[j]`,则`f[i]`一定是`f[j]`的前缀。这个状态能表示子问题之间的关系,可以接着深入下去。 |
| 30 | + |
| 31 | +现在正式开始定义,我们定义状态`f[i]`为第`i`个元素为终点的最长递增子序列的长度,那么状态转移方程是 |
| 32 | + |
| 33 | +`f[j] = max{f[i], 0 <= i < j && f[i] < f[j]} + 1` |
| 34 | + |
| 35 | +有了状态和状态转移方程,代码就不难写了。 |
| 36 | + |
| 37 | +{% if book.java %} |
| 38 | +{% codesnippet "./code/longest-increasing-subsequence-1."+book.suffix, language=book.suffix %}{% endcodesnippet %} |
| 39 | +{% endif %} |
| 40 | + |
| 41 | +{% if book.cpp %} |
| 42 | +```cpp |
| 43 | +// Longest Increasing Subsequence |
| 44 | +// 时间复杂度O(nlogn),空间复杂度O(n) |
| 45 | +class Solution { |
| 46 | +public: |
| 47 | + int lengthOfLIS(vector<int>& nums) { |
| 48 | + if (nums.empty()) return 0; |
| 49 | + // f[i]表示以i结尾的最长递增子序列的长度 |
| 50 | + vector<int> f(nums.size(), 1); |
| 51 | + int global = 1; |
| 52 | + for (int j = 1; j < nums.size(); ++j) { |
| 53 | + for (int i = 0; i < j; ++i) { |
| 54 | + if (nums[i] < nums[j]) { |
| 55 | + f[j] = max(f[j], f[i] + 1); |
| 56 | + } |
| 57 | + } |
| 58 | + global = max(global, f[j]); |
| 59 | + } |
| 60 | + return global; |
| 61 | + } |
| 62 | +}; |
| 63 | +``` |
| 64 | +{% endif %} |
| 65 | +
|
| 66 | +
|
| 67 | +### 解法2 Insert Position |
| 68 | +
|
| 69 | +本题最后有一个进阶问题,能不能`O(n log n)` 解决?有。 |
| 70 | +
|
| 71 | +维护一个单调递增序列,遍历数组,二分查找每一个数在单调序列中的位置,然后替换之。 |
| 72 | +
|
| 73 | +{% if book.java %} |
| 74 | +```java |
| 75 | +// Longest Increasing Subsequence |
| 76 | +// 时间复杂度O(nlogn),空间复杂度O(n) |
| 77 | +public class Solution { |
| 78 | + public int lengthOfLIS(int[] nums) { |
| 79 | + ArrayList<Integer> lis = new ArrayList<>(); |
| 80 | + for (int x : nums) { |
| 81 | + int insertPos = lowerBound(lis, 0, lis.size(), x); |
| 82 | + if (insertPos >= lis.size()) { |
| 83 | + lis.add(x); |
| 84 | + } else { |
| 85 | + lis.set(insertPos, x); |
| 86 | + } |
| 87 | + } |
| 88 | + return lis.size(); |
| 89 | + } |
| 90 | + private static int lowerBound (ArrayList<Integer> A, |
| 91 | + int first, int last, int target) { |
| 92 | + while (first != last) { |
| 93 | + int mid = first + (last - first) / 2; |
| 94 | + if (target > A.get(mid)) first = ++mid; |
| 95 | + else last = mid; |
| 96 | + } |
| 97 | +
|
| 98 | + return first; |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | +{% endif %} |
| 103 | + |
| 104 | +{% if book.cpp %} |
| 105 | +```cpp |
| 106 | +// Longest Increasing Subsequence |
| 107 | +// 时间复杂度O(nlogn),空间复杂度O(n) |
| 108 | +class Solution { |
| 109 | +public: |
| 110 | + int lengthOfLIS(vector<int>& nums) { |
| 111 | + vector<int> lis; |
| 112 | + for (auto x : nums) { |
| 113 | + int insertPos = lower_bound(lis, 0, lis.size(), x); |
| 114 | + if (insertPos >= lis.size()) { |
| 115 | + lis.push_back(x); |
| 116 | + } else { |
| 117 | + lis[insertPos] = x; |
| 118 | + } |
| 119 | + } |
| 120 | + return lis.size(); |
| 121 | + } |
| 122 | + int lower_bound (const vector<int>& A, int first, int last, int target) { |
| 123 | + while (first != last) { |
| 124 | + int mid = first + (last - first) / 2; |
| 125 | + if (target > A[mid]) first = ++mid; |
| 126 | + else last = mid; |
| 127 | + } |
| 128 | + |
| 129 | + return first; |
| 130 | + } |
| 131 | +}; |
| 132 | +``` |
| 133 | +{% endif %} |
0 commit comments