-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDP_contiguousSubArray.java
More file actions
executable file
·43 lines (40 loc) · 1.27 KB
/
DP_contiguousSubArray.java
File metadata and controls
executable file
·43 lines (40 loc) · 1.27 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
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_contiguousSubArray {
int lengthOfSubArray(int []nums){
int mat[][] = new int[nums.length + 1][nums.length + 1];
int [] arr = nums;
int curr_max;
Arrays.sort(arr);
for(int i = 1;i<=nums.length;i++){
curr_max = nums[i-1];
for(int j = 1;j <=nums.length;j++){
if(arr[j-1] == curr_max){
mat[i][j] = mat[i][j-1] + 1;
curr_max++;
}
else{
mat[i][j] = mat[i][j-1];
}
}
if(mat[i][nums.length] < mat[i-1][nums.length]){
mat[i][nums.length] = mat[i-1][nums.length];
}
}
return mat[nums.length][nums.length];
}
public static void main(String[] args) {
int [] nums = {5,4,6,2,3,8};
DP_contiguousSubArray dp = new DP_contiguousSubArray();
int len = dp.lengthOfSubArray(nums);
System.out.println(len);
}
}