Skip to content

Commit fa80eeb

Browse files
committed
Added three questions
1 parent 560c038 commit fa80eeb

15 files changed

Lines changed: 394 additions & 36 deletions

‎java/SUMMARY.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
* [数组](linear-list/array/README.md)
66
* [Remove Duplicates from Sorted Array](linear-list/array/remove-duplicates-from-sorted-array.md)
77
* [Remove Duplicates from Sorted Array II](linear-list/array/remove-duplicates-from-sorted-array-ii.md)
8-
* [Search in Rotated Sorted Array](linear-list/array/search-in-rotated-sorted-array.md)
9-
* [Search in Rotated Sorted Array II](linear-list/array/search-in-rotated-sorted-array-ii.md)
10-
* [Median of Two Sorted Arrays](linear-list/array/median-of-two-sorted-arrays.md)
118
* [Longest Consecutive Sequence](linear-list/array/longest-consecutive-sequence.md)
129
* [Two Sum](linear-list/array/two-sum.md)
1310
* [3Sum](linear-list/array/3sum.md)
@@ -107,7 +104,12 @@
107104
* [查找](search/README.md)
108105
* [Search for a Range](search/search-for-a-range.md)
109106
* [Search Insert Position](search/search-insert-position.md)
107+
* [Search in Rotated Sorted Array](search/search-in-rotated-sorted-array.md)
108+
* [Search in Rotated Sorted Array II](search/search-in-rotated-sorted-array-ii.md)
110109
* [Search a 2D Matrix](search/search-a-2d-matrix.md)
110+
* [Find Minimum in Rotated Sorted Array](search/find-minimum-in-rotated-sorted-array.md)
111+
* [Find Minimum in Rotated Sorted Array II](search/find-minimum-in-rotated-sorted-array-ii.md)
112+
* [Median of Two Sorted Arrays](search/median-of-two-sorted-arrays.md)
111113
* [暴力枚举法](brute-force/README.md)
112114
* [Subsets](brute-force/subsets.md)
113115
* [Subsets II](brute-force/subsets-ii.md)
@@ -145,6 +147,7 @@
145147
* [Triangle](dp/triangle.md)
146148
* [Maximum Subarray](dp/maximum-subarray.md)
147149
* [Maximum Product Subarray](dp/maximum-product-subarray.md)
150+
* [Longest Increasing Subsequence](dp/longest-increasing-subsequence.md)
148151
* [Palindrome Partitioning II](dp/palindrome-partitioning-ii.md)
149152
* [Maximal Rectangle](dp/maximal-rectangle.md)
150153
* [Best Time to Buy and Sell Stock III](dp/best-time-to-buy-and-sell-stock-iii.md)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Find Minimum in Rotated Sorted Array II
2+
// 时间复杂度O(logn),最坏 O(n),空间复杂度O(1)
3+
public class Solution {
4+
public int findMin(int[] nums) {
5+
int left = 0;
6+
int right = nums.length - 1;
7+
while (left < right) {
8+
int mid = left + (right - left) / 2;
9+
if (nums[mid] < nums[right]) {
10+
right = mid;
11+
} else if (nums[mid] > nums[right]) {
12+
left = mid + 1;
13+
} else {
14+
--right;
15+
}
16+
}
17+
return nums[left];
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Find Minimum in Rotated Sorted Array
2+
// 时间复杂度O(logn),空间复杂度O(1)
3+
public class Solution {
4+
public int findMin(int[] nums) {
5+
int left = 0;
6+
int right = nums.length - 1;
7+
while (left < right) {
8+
int mid = left + (right - left) / 2;
9+
if (nums[mid] < nums[right]) {
10+
right = mid;
11+
} else {
12+
left = mid + 1;
13+
}
14+
}
15+
return nums[left];
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Longest Increasing Subsequence
2+
// 时间复杂度O(nlogn),空间复杂度O(n)
3+
class Solution {
4+
public:
5+
int lengthOfLIS(vector<int>& nums) {
6+
if (nums.empty()) return 0;
7+
// f[i]表示以i结尾的最长递增子序列的长度
8+
vector<int> f(nums.size(), 1);
9+
int global = 1;
10+
for (int j = 1; j < nums.size(); ++j) {
11+
for (int i = 0; i < j; ++i) {
12+
if (nums[i] < nums[j]) {
13+
f[j] = max(f[j], f[i] + 1);
14+
}
15+
}
16+
global = max(global, f[j]);
17+
}
18+
return global;
19+
}
20+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Longest Increasing Subsequence
2+
// 时间复杂度O(n^2),空间复杂度O(n)
3+
public class Solution {
4+
public int lengthOfLIS(int[] nums) {
5+
if (nums == null || nums.length == 0) return 0;
6+
// f[i]表示以i结尾的最长递增子序列的长度
7+
int[] f = new int[nums.length];
8+
Arrays.fill(f, 1);
9+
int global = 1;
10+
for (int j = 1; j < nums.length; ++j) {
11+
for (int i = 0; i < j; ++i) {
12+
if (nums[i] < nums[j]) {
13+
f[j] = Math.max(f[j], f[i] + 1);
14+
}
15+
}
16+
global = Math.max(global, f[j]);
17+
}
18+
return global;
19+
}
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Longest Increasing Subsequence
2+
// 时间复杂度O(nlogn),空间复杂度O(n)
3+
class Solution {
4+
public:
5+
int lengthOfLIS(vector<int>& nums) {
6+
vector<int> lis;
7+
for (auto x : nums) {
8+
int insertPos = lower_bound(lis, 0, lis.size(), x);
9+
if (insertPos >= lis.size()) {
10+
lis.push_back(x);
11+
} else {
12+
lis[insertPos] = x;
13+
}
14+
}
15+
return lis.size();
16+
}
17+
int lower_bound (const vector<int>& A, int first, int last, int target) {
18+
while (first != last) {
19+
int mid = first + (last - first) / 2;
20+
if (target > A[mid]) first = ++mid;
21+
else last = mid;
22+
}
23+
24+
return first;
25+
}
26+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Longest Increasing Subsequence
2+
// 时间复杂度O(nlogn),空间复杂度O(n)
3+
public class Solution {
4+
public int lengthOfLIS(int[] nums) {
5+
ArrayList<Integer> lis = new ArrayList<>();
6+
for (int x : nums) {
7+
int insertPos = lowerBound(lis, 0, lis.size(), x);
8+
if (insertPos >= lis.size()) {
9+
lis.add(x);
10+
} else {
11+
lis.set(insertPos, x);
12+
}
13+
}
14+
return lis.size();
15+
}
16+
private static int lowerBound (ArrayList<Integer> A,
17+
int first, int last, int target) {
18+
while (first != last) {
19+
int mid = first + (last - first) / 2;
20+
if (target > A.get(mid)) first = ++mid;
21+
else last = mid;
22+
}
23+
24+
return first;
25+
}
26+
}

‎java/code/search-for-a-range.cpp‎

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,31 @@
33
// 时间复杂度O(logn),空间复杂度O(1)
44
class Solution {
55
public:
6-
vector<int> searchRange (vector<int>& nums, int target) {
7-
auto lower = lower_bound(nums.begin(), nums.end(), target);
8-
auto uppper = upper_bound(lower, nums.end(), target);
6+
vector<int> searchRange (const vector<int>& nums, int target) {
7+
int lower = lower_bound(nums, 0, nums.size(), target);
8+
int upper = upper_bound(nums, 0, nums.size(), target);
99

10-
if (lower == nums.end() || *lower != target)
10+
if (lower == nums.size() || nums[lower] != target)
1111
return vector<int> { -1, -1 };
1212
else
13-
return vector<int> {distance(nums.begin(), lower), distance(nums.begin(), prev(uppper))};
13+
return vector<int> {lower, upper-1};
1414
}
1515

16-
template<typename ForwardIterator, typename T>
17-
ForwardIterator lower_bound (ForwardIterator first,
18-
ForwardIterator last, T value) {
16+
int lower_bound (const vector<int>& A, int first, int last, int target) {
1917
while (first != last) {
20-
auto mid = next(first, distance(first, last) / 2);
21-
22-
if (value > *mid) first = ++mid;
23-
else last = mid;
18+
int mid = first + (last - first) / 2;
19+
if (target > A[mid]) first = ++mid;
20+
else last = mid;
2421
}
2522

2623
return first;
2724
}
2825

29-
template<typename ForwardIterator, typename T>
30-
ForwardIterator upper_bound (ForwardIterator first,
31-
ForwardIterator last, T value) {
26+
int upper_bound (const vector<int>& A, int first, int last, int target) {
3227
while (first != last) {
33-
auto mid = next(first, distance (first, last) / 2);
34-
35-
if (value >= *mid) first = ++mid; // 与 lower_bound 仅此不同
36-
else last = mid;
28+
int mid = first + (last - first) / 2;
29+
if (target >= A[mid]) first = ++mid; // 与 lower_bound 仅此不同
30+
else last = mid;
3731
}
3832

3933
return first;

‎java/code/search-insert-position.cpp‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33
// 时间复杂度O(logn),空间复杂度O(1)
44
class Solution {
55
public:
6-
int searchInsert(vector<int>& nums, int target) {
7-
return distance(nums.begin(), lower_bound(nums.begin(), nums.end(), target));
6+
int searchInsert(const vector<int>& nums, int target) {
7+
return lower_bound(nums, 0, nums.size(), target);
88
}
99

10-
template<typename ForwardIterator, typename T>
11-
ForwardIterator lower_bound (ForwardIterator first,
12-
ForwardIterator last, T value) {
10+
int lower_bound (const vector<int>& A, int first, int last, int target) {
1311
while (first != last) {
14-
auto mid = next(first, distance(first, last) / 2);
15-
16-
if (value > *mid) first = ++mid;
17-
else last = mid;
12+
int mid = first + (last - first) / 2;
13+
if (target > A[mid]) first = ++mid;
14+
else last = mid;
1815
}
1916

2017
return first;
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)