Java very Easy and Concise DP Solution


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

Log in to reply
 

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