-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_175incSub.java
More file actions
38 lines (38 loc) · 1.05 KB
/
_175incSub.java
File metadata and controls
38 lines (38 loc) · 1.05 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
import java.util.*;
public class _175incSub {
public static int lcs(int arr1[], int arr2[]){
int n= arr1.length;
int m= arr2.length;
int dp[][]=new int[n+1][m+1];
for(int i=1;i<n+1;i++){
for(int j=1;j<m+1;j++){
if(arr1[i-1]==arr2[j-1]){
dp[i][j]=dp[i-1][j-1]+1;
} else{
int ans1 = dp[i][j-1];
int ans2 = dp[i-1][j];
dp[i][j]=Math.max(ans1,ans2);
}
}
}
return dp[n][m];
}
public static int lis(int arr1[]){
HashSet<Integer> set = new HashSet<>();
for(int i=0;i<arr1.length;i++){
set.add(arr1[i]);
}
int arr2[] = new int[set.size()];
int i=0;
for(int num : set){
arr2[i] = num;
i++;
}
Arrays.sort(arr2);
return lcs(arr1,arr2);
}
public static void main(String[] args) {
int arr[]={50,3,10,7,40,80};
System.out.println(lis(arr));
}
}