How to determine the longest increasing subsequence using dynamic programming?

前端 未结 19 2643
醉梦人生
醉梦人生 2020-11-22 10:55

I have a set of integers. I want to find the longest increasing subsequence of that set using dynamic programming.

19条回答
  •  眼角桃花
    2020-11-22 11:26

    here is java O(nlogn) implementation

    import java.util.Scanner;
    
    public class LongestIncreasingSeq {
    
    
        private static int binarySearch(int table[],int a,int len){
    
            int end = len-1;
            int beg = 0;
            int mid = 0;
            int result = -1;
            while(beg <= end){
                mid = (end + beg) / 2;
                if(table[mid] < a){
                    beg=mid+1;
                    result = mid;
                }else if(table[mid] == a){
                    return len-1;
                }else{
                    end = mid-1;
                }
            }
            return result;
        }
        
        public static void main(String[] args) {        
            
    //        int[] t = {1, 2, 5,9,16};
    //        System.out.println(binarySearch(t , 9, 5));
            Scanner in = new Scanner(System.in);
            int size = in.nextInt();//4;
            
            int A[] = new int[size];
            int table[] = new int[A.length]; 
            int k = 0;
            while(k A[i]){
                    table[0] = A[i];
                }else if(table[len-1]

    //TreeSet can be used

提交回复
热议问题