- Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathLongestIncreasingSubsequence300.java
More file actions
Latest commit
106 lines (96 loc) · 3.08 KB
/
Copy pathLongestIncreasingSubsequence300.java
File metadata and controls
106 lines (96 loc) · 3.08 KB
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
98
99
100
101
102
103
104
105
106
/**
* 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
* is 4. 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(n log n) time complexity?
*/
publicclassLongestIncreasingSubsequence300 {
publicintlengthOfLIS(int[] nums) {
if (nums == null || nums.length == 0) return0;
intN = nums.length;
int[] dp = newint[N + 1];
intres = 0;
for (inti=1; i<=N; i++) {
dp[i] = 1;
for (intj=1; j<i; j++) {
if (nums[i-1] > nums[j-1]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
if (dp[i] > res) res = dp[i];
}
returnres;
}
/**
* https://leetcode.com/problems/longest-increasing-subsequence/solution/
* https://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/
*/
publicintlengthOfLIS2(int[] nums) {
int[] dp = newint[nums.length];
intlen = 0;
for (intnum : nums) {
inti = Arrays.binarySearch(dp, 0, len, num);
if (i < 0) {
i = -(i + 1);
}
dp[i] = num;
if (i == len) {
len++;
}
}
returnlen;
}
/**
* https://leetcode.com/problems/longest-increasing-subsequence/solution/
*/
publicintlengthOfLIS3(int[] nums) {
intmemo[][] = newint[nums.length + 1][nums.length];
for (int[] l : memo) {
Arrays.fill(l, -1);
}
returnlengthofLIS(nums, -1, 0, memo);
}
publicintlengthofLIS(int[] nums, intprevindex, intcurpos, int[][] memo) {
if (curpos == nums.length) {
return0;
}
if (memo[previndex + 1][curpos] >= 0) {
returnmemo[previndex + 1][curpos];
}
inttaken = 0;
if (previndex < 0 || nums[curpos] > nums[previndex]) {
taken = 1 + lengthofLIS(nums, curpos, curpos + 1, memo);
}
intnottaken = lengthofLIS(nums, previndex, curpos + 1, memo);
memo[previndex + 1][curpos] = Math.max(taken, nottaken);
returnmemo[previndex + 1][curpos];
}
/**
* https://leetcode.com/problems/longest-increasing-subsequence/discuss/74824/JavaPython-Binary-search-O(nlogn)-time-with-explanation
*/
publicintlengthOfLIS4(int[] nums) {
int[] tails = newint[nums.length];
intsize = 0;
for (intx : nums) {
inti = 0, j = size;
while (i != j) {
intm = (i + j) / 2;
if (tails[m] < x)
i = m + 1;
else
j = m;
}
tails[i] = x;
if (i == size) ++size;
}
returnsize;
}
}