-
Notifications
You must be signed in to change notification settings - Fork 0
/
TheLongestIncreasingSubsequence.java
41 lines (34 loc) · 1.11 KB
/
TheLongestIncreasingSubsequence.java
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
import jdk.swing.interop.SwingInterOpUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class TheLongestIncreasingSubsequence {
// Complete the longestIncreasingSubsequence function below.
static int longestIncreasingSubsequence(int[] arr) {
int size = arr.length;
int dp[] = new int[size];
int max = 1;
Arrays.fill(dp,1);
for(int i = size-1;i>=0 ;i--)
for(int j = i+1; j < size;j++)
{
if(arr[i]< arr[j] && dp[i]<dp[j]+1)
{
dp[i]=dp[j]+1;
if (max < dp[i])
max = dp[i];
}
}
return max;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int size = scanner.nextInt();
int[]items = new int[size];
for (int i =0; i<size;i++)
items[i] = scanner.nextInt();
int result = longestIncreasingSubsequence(items);
System.out.println(result);
}
}