MIcrosoft Codility Test
Anonymous User
835

Anyidea for the 1st question? was not able to pass all test cases for 1st
image

image

If this second one can also be optimized-

static int solution(String str) {
int n = str.length();
int[][] dp = new int[n][n];

    // Initialize the dp array
    for (int[] row : dp) {
        Arrays.fill(row, -1);
    }
    //return utilityFunForDel(str, 0, n - 1,dp);
    return lcs(str, 0, n - 1,dp) + 1;

}

private static int lcs(String str, int i, int j, int[][] dp) {
    if (i >= j) {
        return 0;
    }
    if (dp[i][j] != -1) {
        return dp[i][j];
    }
    if (str.charAt(i) == str.charAt(j)) {
        dp[i][j] = 1 + lcs(str, i + 1, j - 1, dp);
    } else {
        dp[i][j] = Math.max(lcs(str, i + 1, j, dp), lcs(str, i, j - 1, dp));
    }
    return dp[i][j];
}
Comments (8)