how to find longest palindromic subsequence?

This can be solved in O(n^2) using dynamic programming. Basically, the problem is about building the longest palindromic subsequence in x[i…j] using the longest subsequence for x[i+1…j], x[i,…j-1] and x[i+1,…,j-1] (if first and last letters are the same). Firstly, the empty string and a single character string is trivially a palindrome. Notice that for a … Read more

Interpretation vs dynamic dispatch penalty in Python

Let’s take a look at this python function: def py_fun(i,N,step): res=0.0 while i<N: res+=i i+=step return res and use ipython-magic to time it: In [11]: %timeit py_fun(0.0,1.0e5,1.0) 10 loops, best of 3: 25.4 ms per loop The interpreter will be running through the resulting bytecode and interpret it. However, we could cut out the interpreter … Read more

Box stacking problem

I think you can solve this using the dynamic programming longest increasing subsequence algorithm: http://www.algorithmist.com/index.php/Longest_Increasing_Subsequence Accounting for the rotations is easy enough: for every tower all you have to check is what happens if you use its height as the length of the base and its width as the height and what happens if you … Read more

What is the difference between memoization and dynamic programming?

Relevant article on Programming.Guide: Dynamic programming vs memoization vs tabulation What is difference between memoization and dynamic programming? Memoization is a term describing an optimization technique where you cache previously computed results, and return the cached result when the same computation is needed again. Dynamic programming is a technique for solving problems of recursive nature, … Read more

What is the difference between bottom-up and top-down?

rev4: A very eloquent comment by user Sammaron has noted that, perhaps, this answer previously confused top-down and bottom-up. While originally this answer (rev3) and other answers said that “bottom-up is memoization” (“assume the subproblems”), it may be the inverse (that is, “top-down” may be “assume the subproblems” and “bottom-up” may be “compose the subproblems”). … Read more