How to determine the longest increasing subsequence using dynamic programming?

前端 未结 19 2582
醉梦人生
醉梦人生 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条回答
  •  -上瘾入骨i
    2020-11-22 11:30

    This is a Java implementation in O(n^2). I just did not use Binary Search to find the smallest element in S, which is >= than X. I just used a for loop. Using Binary Search would make the complexity at O(n logn)

    public static void olis(int[] seq){
    
        int[] memo = new int[seq.length];
    
        memo[0] = seq[0];
        int pos = 0;
    
        for (int i=1; i= x){
                            memo[j] = x;
                            break;
                        }
                    }
                }
                //just to print every step
                System.out.println(Arrays.toString(memo));
        }
    
        //the final array with the LIS
        System.out.println(Arrays.toString(memo));
        System.out.println("The length of lis is " + (pos + 1));
    
    }
    

提交回复
热议问题