Questions tagged [dynamic-programming]
Dynamic programming is an algorithmic technique for efficiently solving problems which have recursive structure with many overlapping subproblems. Do NOT use this tag for general "dynamic" behavior in code.
dynamic-programming
5,695
questions
362
votes
12
answers
144k
views
What is the difference between memoization and dynamic programming?
What is the difference between memoization and dynamic programming? I think dynamic programming is a subset of memoization. Is it right?
291
votes
10
answers
146k
views
What is dynamic programming? [closed]
What is dynamic programming?
How is it different from recursion, memoization, etc?
I have read the wikipedia article on it, but I still don't really understand it.
275
votes
9
answers
223k
views
What is the difference between bottom-up and top-down?
The bottom-up approach (to dynamic programming) consists in first looking at the "smaller" subproblems, and then solve the larger subproblems using the solution to the smaller problems.
The top-down ...
234
votes
21
answers
203k
views
How to determine the longest increasing subsequence using dynamic programming?
I have a set of integers. I want to find the longest increasing subsequence of that set using dynamic programming.
218
votes
10
answers
140k
views
Difference between Divide and Conquer Algo and Dynamic Programming
What is the difference between Divide and Conquer Algorithms and Dynamic Programming Algorithms? How are the two terms different? I do not understand the difference between them.
Please take a simple ...
151
votes
8
answers
16k
views
Throwing cats out of windows
Imagine you're in a tall building with a cat. The cat can survive a fall out of a low story window, but will die if thrown from a high floor. How can you figure out the longest drop that the cat can ...
118
votes
6
answers
51k
views
Why is the knapsack problem pseudo-polynomial?
I know that Knapsack is NP-complete while it can be solved by DP. They say that the DP solution is pseudo-polynomial, since it is exponential in the "length of input" (i.e. the numbers of bits ...
115
votes
20
answers
185k
views
Find common substring between two strings
I'd like to compare 2 strings and keep the matched, splitting off where the comparison fails.
So if I have 2 strings:
string1 = "apples"
string2 = "appleses"
answer = "apples&...
97
votes
5
answers
93k
views
A simple example for someone who wants to understand Dynamic Programming [closed]
I am looking for a manageably understandable example for someone who wants to learn Dynamic Programming. There are nice answers here about what is dynamic programming. The fibonacci sequence is a ...
84
votes
4
answers
14k
views
What is the minimum cost to connect all the islands?
There is a grid of size N x M. Some cells are islands denoted by '0' and the others are water. Each water cell has a number on it denoting the cost of a bridge made on that cell. You have to find the ...
75
votes
5
answers
77k
views
What's the difference between recursion, memoization & dynamic programming? [duplicate]
Related question:
Dynamic programming and memoization: top-down vs bottom-up approaches
I have gone through a lot of articles on this but can't seem to make sense of it. At times recursion and ...
65
votes
9
answers
31k
views
Find the number of occurrences of a subsequence in a string
For example, let the string be the first 10 digits of pi, 3141592653, and the subsequence be 123. Note that the sequence occurs twice:
3141592653
1 2 3
1 2 3
This was an interview question ...
65
votes
11
answers
74k
views
Getting the submatrix with maximum sum?
Input: A 2-dimensional array NxN - Matrix - with positive and negative elements.Output: A submatrix of any size such that its summation is the maximum among all possible submatrices.
Requirement: ...
63
votes
4
answers
23k
views
Is there a generic way to memoize in Scala?
I wanted to memoize this:
def fib(n: Int) = if(n <= 1) 1 else fib(n-1) + fib(n-2)
println(fib(100)) // times out
So I wrote this and this surprisingly compiles and works (I am surprised because ...
60
votes
1
answer
18k
views
How to count integers between large A and B with a certain property?
In programming contests, the following pattern occurs in a lot of tasks:
Given numbers A and B that are huge (maybe 20 decimal digits or more), determine the number of integers X with A ≤ X ≤ B ...