DP Java solution


  • 0
    T
    class Solution {
        
        int max = 0;
        public int findLength(int[] A, int[] B) {
            if(A.length<1 || B.length<1){
                return 0;
            }
            
            int[][] dp = new int[A.length+1][B.length+1];
            for(int i=1;i<=A.length;i++){
                for(int j=1;j<=B.length;j++){
                    if(A[i-1]==B[j-1]){
                        dp[i][j] = 1+dp[i-1][j-1];
                    }else{
                        dp[i][j]=0;
                    }
                    max = Math.max(dp[i][j],max);
                }
            }
            return max;
        }
    
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.