dynamic-programming

Memoization Handler

Is it "good practice" to create a class like the one below that can handle the memoization process for you? The benefits of memoization are so great (in some cases, like this one, where it drops from 501003 to 1507 function calls and from 1.409 to 0.006 seconds of CPU time on my computer) that it seems a class like this would be useful. ...

Dynamic programming idiom for combinations

Consider the problem in which you have a value of N and you need to calculate how many ways you can sum up to N dollars using [1,2,5,10,20,50,100] Dollar bills. Consider the classic DP solution: C = [1,2,5,10,20,50,100] def comb(p): if p==0: return 1 c = 0 for x in C: if x <= p: c += comb(p-x) ...

Generate a message out of cutout magazine characters (interview question)

This problem comes out of the dynamic programming chapter in The Algorithm Deisgn Manual by Skiena. Give an algorithm to determine whether you can generate a given string by pasting cutouts from a magazine. You are given a function that will identify the character and its position on the reverse side of the page for any given charac...

How to split a string into words. Ex: "stringintowords" -> "String Into Words" ?

What is the right way to split a string into words ? (string doesn't contain any spaces or punctuation marks) For example: "stringintowords" -> "String Into Words" Could you please advise what algorithm should be used here ? ! Update: For those who think this question is just for curiosity. This algorithm could be used to camеlcase do...

Most effecient way to compute a series of moves in peg solitaire.

Given an arbitary peg solitaire board configuration, what is the most effecient way to compute any series of moves that results in the "end game" position. For example, the standard starting position is: ..***.. ..***.. ******* ***O*** ******* ..***.. ..***.. And the "end game" position is: ..OOO.. ..OOO.. OOOOOOO OOO*OOO OOOOOOO .....

efficient longest common subsequence algorithm library?

I'm looking for a (space) efficient implementation of an LCS algorithm for use in a C++ program. Inputs are two random access sequences of integers. I'm currently using the dynamic programming approach from the wikipedia page about LCS. However, that has O(mn) behaviour in memory and time and dies on me with out of memory errors for larg...

How to dynamic new Anonymous Class ?

In C# 3.0 you can create anonymous class with the following syntax var o1 = new { Id = 1, Name = "Foo" }; Is there a way to dynamic create these anonymous class to a variable? Example: var o1 = new { Id = 1, Name = "Foo" }; var o2 = new { SQ = 2, Birth = DateTime.Now }; Dynamic create Example: var o1 = DynamicNewAnonymous(new...

Contiguous All-one block in a matrix

Hi, the problem is - Suppose you are given an mXn bitmap, represented by an array M[1..m,1.. n] whose entries are all 0 or 1. A all-one block is a subarray of the form M[i .. i0, j .. j0] in which every bit is equal to 1. Describe and analyze an efficient algorithm to find an all-one block in M with maximum area I am trying to make a ...

finding maximum size sub-matrix of all 1's in a matrix having 1's and 0's

Hi, the problem is - Suppose you are given an mXn bitmap, represented by an array M[1..m,1.. n] whose entries are all 0 or 1. A all-one block is a subarray of the form M[i .. i0, j .. j0] in which every bit is equal to 1. Describe and analyze an efficient algorithm to find an all-one block in M with maximum area I am trying to make a d...

Is this problem NP-hard?

I'm trying to come up with a reasonable algorithm for this problem: Let's say you have a bunch of balls. Each ball has at least one color, but can also be multicolored. Each ball also has a number on it. There are also a bunch of boxes which are each only one color. The goal is to maximize the sum of the numbers on the balls in the ...

Efficient Method for Calculating the Probability of a Set of Outcomes?

Let's say I'm playing 10 different games. For each game, I know the probability of winning, the probability of tying, and the probability of losing (each game has different probabilities). From these values, I can calculate the probability of winning X games, the probability of losing X games, and the probability of tying X games (for ...