shlogg · Early preview
Prashant Mishra @prashantrmishra

Longest Increasing Subsequence Problem Solution

Longest Increasing Subsequence problem solved using dynamic programming with tabulation and memoization methods in Java.

Problem

class Solution {
    // Function to find length of longest increasing subsequence.
    static int longestSubsequence(int n, int a[]) {
        // tabulation method (bottom up ) 
        int dp[][] = new int[a.length+1][a.length+1];
        for(int index = a.length-1;index>=0;index--){
            for(int prev = a.length-1;prev>=-1;prev--){
                int take  = 0;
                if(prev ==-1 || a[prev] < a[index]){
                    take = Integer.max(take , 1 + dp[index+1][index+1]);
                }
                int dontTake  = dp[index+1][prev+1];
                dp[in...