Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
97 lines (92 loc) · 2.52 KB

File metadata and controls

97 lines (92 loc) · 2.52 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package problems;
import java.util.Arrays;
/**
* dp[i]为考虑前i个元素,以第i个数字结尾的最长上升子序列的长度。
* 在计算dp[i]之前,我们已经计算出dp[0...i-1]的值,则状态转移方程为:
* dp[i] = max(dp[j])+1, 其中 0<=j<i 且 num[j] < num[i]
* 最后数组的最大上升子序列即所有dp[i]中的最大值
* LIS_length = max(dp[i]),其中0<=i<n
*/
public class LeetCode300 {
public int lengthOfLIS(int[] nums) {
if(nums.length == 0){
return 0;
}
int[] dp = new int[nums.length];
dp[0] = 1;
int maxAns = 1;
for (int i = 1; i < nums.length; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if(nums[i] > nums[j]){
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
maxAns = Math.max(dp[i], maxAns);
}
return maxAns;
}
}
/**
* 最长上升子序列
*/
class LeetCode300_1 {
public int lengthOfLIS(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
dp[0] = 1;
int maxans = 1;
for (int i = 1; i < n; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if(nums[i] > nums[j]) {
dp[i] = Math.max(dp[j] + 1, dp[i]);
}
}
maxans = Math.max(maxans, dp[i]);
}
return maxans;
}
}
class LeetCode300_2 {
public int lengthOfLIS(int[] nums) {
if(nums.length == 0) {
return 0;
}
int[] dp = new int[nums.length];
int res = 1;
Arrays.fill(dp, 1);
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if(nums[i] > nums[j]) {
dp[i] = Math.max(dp[j] + 1, dp[i]);
}
}
res = Math.max(res, dp[i]);
}
return res;
}
public int lengthOfLIS1(int[] nums) {
if(nums.length == 0){
return 0;
}
int[] tails = new int[nums.length];
int res = 0;
for (int num : nums) {
int i = 0, j = res;
while (i < j) {
int mid = (i + j) / 2;
if(tails[mid] < num) {
i = mid + 1;
} else {
j = mid;
}
}
tails[i] = num;
if(res == j){
res ++;
}
}
return res;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.