Questions tagged [dynamic-programming]

Dynamic programming builds solutions from solutions to simpler subproblems. It's closely allied to recursion, but dynamic programming algorithms are formulated as iteration usually over a very regular datastructure.

The link between dynamic programming and recursion is actually very strong.

Any dynamic programming solution can be transformed into a recursive solution (with memoization), with identical performance characteristics, e.g O(n*n). The difference is primarily one of presentation.

The longest common subsequence problem is a good example of a problem that can be solved with dynamic programming.

67 questions
19
votes
5 answers

How do you identify a problem as being suitable for dynamic programming?

I have been reading up on dynamic programming lately. Would like to hear from someone who started from scratch and now is pretty good at identifying and solving DP problems. I am struggling in identifying these problems as DP and framing a concise…
user110036
  • 305
  • 2
  • 6
9
votes
3 answers

number of strings, when each character must occur even times

I've been bashing my skull at this problem for some time now, and its really starting to frustrate me. The problem is: I have a set of characters, A, B, C, and D. I have to tell in how many ways a string can be built from those characters, when the…
Olavi Mustanoja
  • 223
  • 1
  • 4
9
votes
2 answers

How to get better at solving Dynamic programming problems

I recently came across this question: "You are given a boolean expression consisting of a string of the symbols 'true', 'false', 'and', 'or', and 'xor'. Count the number of ways to parenthesize the expression such that it will evaluate to true. For…
newbie
  • 93
  • 1
  • 4
9
votes
2 answers

How is the "Pizza picking problem‍​" solved using dynamic programming techniques?

Winkler's pizza picking problem: A circular pizza pie of n slices, where slice i has area S_i i.e, the area is different for each pie piece. Eaters Alice and Bob take turns picking slices, but it is rude to create multiple gaps in the pie…
DrDeltaS
  • 91
  • 1
  • 3
8
votes
3 answers

Longest subsequence without string

Does there exist a dynamic programming algorithm to find the longest subsequence in a string X that does not contain Y as substring? Just that this problem seems so similar to other DP string algorithms such as longest common subsequence and string.…
user83834
  • 81
  • 4
7
votes
1 answer

Finding all possible ways of inserting a pattern into a string

I've been thinking about this problem for a while, and I can only find a recursive solution but I'm feeling that there is a dynamic programming way to do it, I just can't figure it out. Is this a famous problem I don't know about? Q: Given a string…
7
votes
3 answers

Picking m number in the best possible time

Imagine we want to pick m numbers from n numbers so that the difference of the maximum and minimum of the m numbers be minimum, for example if m = 4 n =6 numbers: 10 12 10 7 5 22 The minimum difference is 5, picking 5, 7, 10, 10 from the…
Amen
  • 135
  • 8
7
votes
3 answers

Number of strings containing a specific substring

I've seen numerous questions (and answers) concerning the number of binary strings (e.g "10010" containing some binary substring (e.g "00"). I'd like to know if there is a way to generalize this: Given a length n and a string s (may contain any…
6
votes
5 answers

Are mocks in unit tests dangerous in dynamic languages?

I've started relying heavily on a mocking framework in php for my unit tests. My concern is that with a dynamic language, there is no way of enforcing a return type. When mocking, you have to ensure that the return type of your mocked method is…
GWed
  • 3,085
  • 5
  • 26
  • 43
6
votes
1 answer

Help/suggestions for Parallel assembly line scheduling (Dynamic programming)

I am working on a problem similar to the assembly line scheduling by dynamic programming.The issue is that unlike the classic problem where we have predefined stations now I only have information which task should run before which other(could be…
user109405
5
votes
5 answers

My brute force solution is too slow, needed DP solution

Concise problem definition: given n and {a,b,c}; (1 ≤ n, a, b, c ≤ 4000); Constraint -> a*i + b*j + c*k==n (i,j,k>=0); Objective-> maximize(i,j,k) Examples: n=47 and a=7,b=5,c=8 -> max=9 (i=1,j=8,k=0) == 7*1+5*8+8*0=47 n=7 and a=5,b=5,c=2 ->…
5
votes
1 answer

Dynamic Programming - Largest arrangement of bookcases

I'm trying to solve a problem, so I'm not looking for code, but for similar algorithms so I can solve it myself. I am given n bookcases each with a size amount of books inside. I am to move some of these bookcases to a new room as follows: The…
Xzenon
  • 167
  • 3
5
votes
3 answers

Find path of steepest descent along with path length in a matrix

Came across this problem -- You are given a grid with numbers that represent the elevation at a particular point. From each box in the grid you can go north, south, east, west - but only if the elevation of the area you are going into is less than…
WeirdAl
  • 79
  • 4
5
votes
2 answers

Looking for a DP algorithm for a specific packing problem

I have the following problem to solve: Given a ferry with length d and a list of n cars conataining their length we must load the cars on the ferry in an order in which they appear in the list on 2 different lanes (each one having length d). Write a…
qiubit
  • 205
  • 2
  • 5
5
votes
4 answers

The optimal recipe problem

Suppose I have a list of Ingredients In My Fridge which are going off soon, and a list of Recipes which use up various Ingredients. (Some of which I don't currently have.) Is there an algorithm which produces the optimal set of Recipes, where an…
Simon Cozens
  • 161
  • 3
1
2 3 4 5