Wednesday, December 24, 2014

Longest palindromic subsequence of an array

Write a function to compute the maximum length palindromic sub-sequence of an array. 
A palindrome is a sequence which is equal to its reverse. 
A sub-sequence of an array is a sequence which can be constructed by removing elements of the array. 
Ex: Given [4,1,2,3,4,5,6,5,4,3,4,4,4,4,4,4,4] should return 10 (all 4's) 


Well, I think my whole Christmas eve is filled with DP, palindromes and strings (this one is sort of one...). This is a Linkedin interview question. The solution, as far as I can think of, is DP (again). I had some problem figuring out how to construct the DP matrix. It is an upper (or lower) diagonal matrix with the rows as the start character and columns as the end characters.

Here is a sample matrix, I use string instead of those annoying numbers.
S = "BBABA"



public class PalindromicSubsequence {
 public int maxLengthPalindrome(int[] nums) {
  if (nums == null || nums.length == 0) 
   return 0;
  int[][] palindrome = new int[nums.length][nums.length];
  for (int i = 0; i < nums.length; i++) {
   palindrome[i][i] = 1;
  }
  for (int i = 0; i < nums.length - 1; i++) {
   palindrome[i][i + 1] = nums[i] == nums[i + 1] ? 2 : 1;
  }
  for (int len = 3; len <= nums.length; len++) {
   for (int start = 0; start <= nums.length - len; start++) {
    int end = start + len - 1;
    palindrome[start][end] = Math.max(palindrome[start][end - 1], palindrome[start + 1][end]);
    if (nums[start] == nums[end]) {
     palindrome[start][end] = Math.max(palindrome[start][end], palindrome[start + 1][end - 1] + 2);
    }
   }
  }
  return palindrome[0][nums.length - 1]; 
 }
}

1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete