-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDP_NoOfLongestIncreasingSubSequence.java
More file actions
executable file
·52 lines (51 loc) · 1.55 KB
/
DP_NoOfLongestIncreasingSubSequence.java
File metadata and controls
executable file
·52 lines (51 loc) · 1.55 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
import java.util.Arrays;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Neel_Kapadia
*/
public class DP_NoOfLongestIncreasingSubSequence {
public int findNumberOfLIS(int[] nums) {
if(nums.length==0)
return 0;
if(nums.length==1)
return 1;
int lis[] = new int[nums.length];
Arrays.fill(lis, 1);
System.out.println(lis[0]);
for (int i = 1; i < lis.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j] && lis[i] < lis[j] + 1) {
lis[i] = lis[j] + 1;
}
}
System.out.println(lis[i]);
}
int max_count = 0, count;
for (int i = lis.length - 1; i > 0; i--) {
count = 1;
while (lis[i] == lis[i - 1]) {
count++;
if (i > 1) {
i--;
} else {
break;
}
}
if (count > max_count) {
max_count = count;
}
}
return max_count;
}
public static void main(String[] args) {
DP_NoOfLongestIncreasingSubSequence dp = new DP_NoOfLongestIncreasingSubSequence();
int nums[] = {1,2,4,3,5,4,7,2};
int x = dp.findNumberOfLIS(nums);
System.out.println("Count: "+x);
}
}