-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestIncreasingSubsequence.java
More file actions
42 lines (32 loc) · 964 Bytes
/
LongestIncreasingSubsequence.java
File metadata and controls
42 lines (32 loc) · 964 Bytes
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
package com.mycompany.algorithm_final_project;
import java.util.Scanner;
/**
*
* @author israkkayumchowdhury
*/
public class LongestIncreasingSubsequence {
static int lis(int i, int[] a){
int ans = 1;
for (int j = 0; j < i; j++) {
if (a[i] > a[j]) {
ans = Math.max(ans, lis(j, a) + 1);
}
}
return ans;
}
public void main_func(){
Scanner s = new Scanner(System.in);
System.out.print(" Enter the elements size: ");
int n = s.nextInt();
int[] nums = new int[n];
System.out.print(" Enter the elements: ");
for (int i = 0; i < n; i++) {
nums[i] = s.nextInt();
}
int ans = 0;
for(int i = 0; i < n; i++){
ans = Math.max(ans, lis(i, nums));
}
System.out.print(" Longest Increasing Subsequence Length: " + ans);
}
}