-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestIncresingSubsequence.java
More file actions
35 lines (35 loc) · 981 Bytes
/
longestIncresingSubsequence.java
File metadata and controls
35 lines (35 loc) · 981 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
package problems.dynamicprogramming;
import java.util.*;
public class longestIncresingSubsequence {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t > 0){
int n = sc.nextInt();
int array[] = new int[n];
for (int i = 0; i < n; ++i)
{
array[i] = sc.nextInt();
}
Solution ob = new Solution();
System.out.println(ob.longestSubsequence(n,array));
t--;
}
}
}
class Solution {
// return length of longest strictly increasing subsequence
static int longestSubsequence(int n, int a[]){
int dp[]=new int[n];
int len=0;
for(int num:a){
int i=Arrays.binarySearch(dp,0,len,num);
if(i<0)
i=-(i+1);
dp[i]=num;
if(i==len)
len++;
}
return len;
}
}