-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_300.java
More file actions
35 lines (34 loc) · 892 Bytes
/
a_300.java
File metadata and controls
35 lines (34 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package dynamicProgramming;
/**
* 最长递增子序列
*/
public class a_300 {
public int lengthOfLIS(int[] nums) {
int[] tails = new int[nums.length];
int len = 0;
for (int num : nums) {
int index = binarySearch(tails, len, num);
tails[index] = num;
if (index == len) {
len++;
}
}
return len;
}
private int binarySearch(int[] tails, int len, int key) {
int l = 0, h = len;
while (l < h) {
int mid = l + (h - l) / 2;
if (tails[mid] == key) {
return mid;
} else if (tails[mid] > key) {
//错误h = mid - 1;
//查找 Si 位于 tails 数组的位置
h = mid;
} else {
l = mid + 1;
}
}
return l;
}
}