- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLIS.java
More file actions
Latest commit
72 lines (66 loc) · 1.9 KB
/
Copy pathLIS.java
File metadata and controls
72 lines (66 loc) · 1.9 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//http://www.geeksforgeeks.org/dynamic-programming-set-3-longest-increasing-subsequence/
importjava.util.*;
importjava.lang.*;
importjava.io.*;
importjava.util.Arrays;
classGFG {
publicstaticvoidmain (String[] args) throwsIOException{
Reader.init(System.in);
intT = Reader.nextInt();
while(T-->0){
intN = Reader.nextInt();
int[] arr = newint[N];
for(inti = 0; i < N; i++){
arr[i] = Reader.nextInt();
}
System.out.println(LIS(arr,N));
}
}
publicstaticintLIS(int[] arr, intN){
int[] memo = newint[N];
Arrays.fill(memo, 1);
for(inti = 1; i < N; i++){
for(intj = i-1; j >= 0; j--){
if(arr[i] > arr[j]){
memo[i] = max(1+memo[j], memo[i]);
}
}
}
intmax = Integer.MIN_VALUE;
for(inti = 0; i < N; i++){
max = max(memo[i], max);
}
returnmax;
}
publicstaticintmax(inta, intb){
returna > b ? a : b;
}
}
classReader {
staticBufferedReaderreader;
staticStringTokenizertokenizer;
/** call this method to initialize reader for InputStream */
staticvoidinit(InputStreaminput) {
reader = newBufferedReader(
newInputStreamReader(input) );
tokenizer = newStringTokenizer("");
}
/** get next word */
staticStringnext() throwsIOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = newStringTokenizer(
reader.readLine() );
}
returntokenizer.nextToken();
}
staticintnextInt() throwsIOException {
returnInteger.parseInt( next() );
}
staticlongnextLong() throwsIOException{
returnLong.parseLong( next() );
}
staticdoublenextDouble() throwsIOException {
returnDouble.parseDouble( next() );
}
}