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

Maximum possible number of rectangles that can be crossed with a single straight line

Here is a sketch of an O(n^2 log n) solution. First, the preliminaries shared with other answers. When we have a line passing through some rectangles, we can translate it to any of the two sides until it passes through a corner of some rectangle. After that, we fix that corner as the center of … Read more

How to understand the dynamic programming solution in linear partitioning?

Be aware that there’s a small mistake in the explanation of the algorithm in the book, look in the errata for the text “(*) Page 297”. About your questions: No, the items don’t need to be sorted, only contiguous (that is, you can’t rearrange them) I believe the easiest way to visualize the algorithm is … Read more

Smallest number that cannot be formed from sum of numbers from array

There’s a beautiful algorithm for solving this problem in time O(n + Sort), where Sort is the amount of time required to sort the input array. The idea behind the algorithm is to sort the array and then ask the following question: what is the smallest positive integer you cannot make using the first k … Read more

Given a list of numbers and a number k, return whether any two numbers from the list add up to k

This question can be easily solved with the help of set in O(N) time and space complexity.First add all the elements of array into set and then traverse each element of array and check whether K-ar[i] is present in set or not. Here is the code in java with O(N) complexity : boolean flag=false; HashSet<Long> … Read more

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)