Skip to content

Commit edb6ef0

Browse files
committed
Added Longest increasing subsequence example (dp)
1 parent b17c43b commit edb6ef0

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

Dynamic_Programming.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.io.PrintWriter;
4+
import java.util.Arrays;
5+
6+
/**
7+
*
8+
* @author pulkit4tech
9+
*/
10+
public class Dynamic_Programming implements Runnable {
11+
12+
BufferedReader c;
13+
PrintWriter pout;
14+
// static long mod = 1000000007;
15+
16+
public void run() {
17+
try {
18+
c = new BufferedReader(new InputStreamReader(System.in));
19+
pout = new PrintWriter(System.out, true);
20+
solve();
21+
pout.close();
22+
} catch (Exception e) {
23+
pout.close();
24+
e.printStackTrace();
25+
System.exit(1);
26+
}
27+
}
28+
29+
public static void main(String[] args) throws Exception {
30+
new Thread(new Dynamic_Programming()).start();
31+
}
32+
33+
void solve() throws Exception {
34+
longest_increasing_subsequence();
35+
}
36+
37+
private void longest_increasing_subsequence() {
38+
int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 };
39+
int n = arr.length;
40+
System.out.println("Length of lis is "
41+
+ lis(arr, n) + "\n");
42+
}
43+
44+
int lis(int arr[],int n){
45+
int lis[] = new int[n];
46+
int i,j,max = 0;
47+
48+
for (i=0;i<n;i++)
49+
lis[i] = 1;
50+
51+
for(i=1;i<n;i++){
52+
for(j=0;j<i;j++){
53+
if(arr[i]>arr[j]&&lis[i]<lis[j]+1)
54+
lis[i] = lis[j]+1;
55+
}
56+
}
57+
58+
for(i=0;i<n;i++)
59+
if(max<lis[i])
60+
max=lis[i];
61+
62+
return max;
63+
}
64+
65+
66+
}

0 commit comments

Comments
 (0)