-
Notifications
You must be signed in to change notification settings - Fork 43
/
LongestIncreasingSubsequence.java
124 lines (111 loc) · 3.31 KB
/
LongestIncreasingSubsequence.java
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package LeetCodeJava.BinarySearch;
// https://leetcode.com/problems/longest-increasing-subsequence/description/
import java.util.Arrays;
public class LongestIncreasingSubsequence {
// V0
// IDEA : DP
// TODO : check & implment again
public int lengthOfLIS(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
int n = nums.length;
// init dp
int[] dp = new int[nums.length];
/**
*
* NOTE !!!
*
* Arrays.fill(dp, 1);
* -> sets every element in the array dp to the value 1.
*
*/
Arrays.fill(dp,1);
// set res as final result (Longest Increasing Subsequence length) , keep updating it
int res = 1;
/**
* NOTE !!!
*
* main dp logic : dp[i]=Math.max(dp[i],dp[j]+1);
*
* -> we still use "brute force", double looping over i, j
* However, we use dp to "memorize" the result already calculated before
*
*
* i start from 1, while j start from 0
*/
for(int i=1; i<n; i++){
for(int j=0; j<i; j++){
if(nums[i]>nums[j]){
dp[i] = Math.max(dp[i],dp[j]+1);
res = Math.max(res, dp[i]);
}
}
}
return res;
}
// V1
// IDEA : DP
// https://leetcode.com/problems/longest-increasing-subsequence/solutions/4509493/300/
public int lengthOfLIS_1(int[] nums) {
if(nums == null || nums.length == 0)return 0;
int n=nums.length;
int[] dp=new int[n];
Arrays.fill(dp,1);
for(int i=1;i<n;i++){
for(int j=0;j<i;j++){
if(nums[i]>nums[j]){
dp[i]=Math.max(dp[i],dp[j]+1);
}
}
}
int maxi=1;
for(int len : dp){
maxi=Math.max(maxi,len);
}
return maxi;
}
// V2
// IDEA : BINARY SEARCH
// https://leetcode.com/problems/longest-increasing-subsequence/solutions/4509303/beats-100-binary-search-explained-with-video-c-java-python-js/
public int lengthOfLIS_2(int[] nums) {
int[] tails = new int[nums.length];
int size = 0;
for (int x : nums) {
int i = 0, j = size;
while (i != j) {
int m = (i + j) / 2;
if (tails[m] < x)
i = m + 1;
else
j = m;
}
tails[i] = x;
if (i == size) ++size;
}
return size;
}
// V3
// IDEA : DP
// https://leetcode.com/problems/longest-increasing-subsequence/solutions/4510776/java-solution-explained-in-hindi/
public int lengthOfLIS_3(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int n = nums.length;
int[] dp = new int[n];
Arrays.fill(dp, 1);
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
int maxLength = 0;
for (int len : dp) {
maxLength = Math.max(maxLength, len);
}
return maxLength;
}
}