Skip to main content
18 votes

Dynamic Programming vs Memoization

Summary: the memoization technique is a routine trick applied in dynamic programming (DP). In contrast, DP is mostly about finding the optimal substructure in overlapping subproblems and establishing ...
喜欢算法和数学's user avatar
13 votes

Find 1s in almost all 0 array using comparisons only

Start by splitting the 100 elements in 50 pairs of two, and use a comparison on each pair. If a comparison returns -1 or 1, then you've found one of the elements which is 1 (and the other one must be ...
orlp's user avatar
  • 14k
11 votes
Accepted

Why does the order of the nested loops matter when solving the Coin Change problem?

In the second solution you are overcounting the number of combinations. In fact you are counting the of ways to choose an ordered list of coins that sums to sum. ...
Steven's user avatar
  • 29.8k
9 votes
Accepted

Counting number of paths between two vertices in a DAG

Here is a dynamic programming algorithm. Given a graph $G = (V, E)$ and two vertices $u, v \in V$. We define the recursive function $C:V\rightarrow \mathbb{N}$, such that $C(w)$ is the number of paths ...
Narek Bojikian's user avatar
9 votes

Find the number using binary search against one possible lie

A generalization of this class of problems is widely studied. See, e.g., this paper for a survey. In your particular case, the problem can be easily solved without any asymptotic change in the ...
Steven's user avatar
  • 29.8k
8 votes
Accepted

Is dynamic programming restricted to optimization problems?

Dynamic Programming can be used to solve a problem as long as the problem has a recursive substructure and the sub-structural problems are overlapping. So, as long as a problem has the two properties, ...
Arkajyoti Banerjee's user avatar
7 votes
Accepted

Find the lexicographically smallest order of N numbers such that the total of N numbers <= Threshold value

You are on the right track. It turns out the original question can be solved by a greedy algorithm. (A full blown solution by dynamic programming as I tried a while ago is both an overkill on coding ...
喜欢算法和数学's user avatar
7 votes
Accepted

Calculating all products of $n-1$ factors when given $n$ factors

Here is the fastest algorithm. I bet. The idea of the algorithm can be seen from the one-line explanation between step 4 and step 5 below. Input: $e_1,\cdots,e_n\in E$, where $n\ge 3$. Output: $p_1, \...
喜欢算法和数学's user avatar
7 votes
Accepted

Find if there is matrix that satisfying the following conditions

Hint #1:
D.W.'s user avatar
  • 169k
7 votes

Find the number using binary search against one possible lie

If normal binary search would take k questions, then you can solve this with 2k+1 questions: Ask each question twice. If you get the same answer, it was the truth. If not, a third question reveals the ...
gnasher729's user avatar
  • 32.6k
6 votes
Accepted

Word Problem over Finite Groupoids

This word problem over groupoid is a nice example to show the essence of dynamic programming, the recognition of the subproblems. In other words, how can we define the table or the multi-dimensional ...
喜欢算法和数学's user avatar
6 votes

Why does the order of the nested loops matter when solving the Coin Change problem?

Why is the working solution correct? First things first. Important things first. Let us understand why the working solution is correct first instead of finding where the wrong solution goes wrong. ...
喜欢算法和数学's user avatar
6 votes

Set of K elements with minimum cost and GCD=1

You can do this with dynamic programming. Let $S$ be the input set, and let $N=\max S$. We define the table $T\in\mathbb{N}^N$ where $$ T[w] = \min_{\substack{X\subseteq S\\ \operatorname{gcd}(A[X])=w}...
Narek Bojikian's user avatar
6 votes
Accepted

Why does Levenshtain Distance take into account DP[i-1][j-1] when computing the minimum, if LCS doesn't when computing the maximum?

The "DP[i - 1][j - 1]" condition is for aligning two different characters. You can do this in the Levenshtein Distance algorithm but not in LCS. For ...
proof-of-correctness's user avatar
5 votes

How does matrix chain multiplication problem has an optimal substructure?

First of all, it is correct and important to know that optimal solutions need not necessarily be unique! However, this doesn't mean we cannot have optimal substructure. Recall what optimal ...
Discrete lizard's user avatar
  • 8,472
5 votes

Dynamic Programming vs Memoization

Memoization is the technique to "remember" the result of a computation, and reuse it the next time instead of recomputing it, to save time. It does not care about the properties of the computations. ...
Yufan Lou's user avatar
  • 138
5 votes

Do all recursive problems have optimal substructure?

I think there is some confusion in terminology here. Part of the problem is what we mean by problem and algorithm. But there is more to it: Textbooks like CLRS introduce the notion of optimal ...
Amelio Vazquez-Reina's user avatar
5 votes
Accepted

How can Lagrange Multipliers and Penalty Method be applied to optimize algorithms?

This technique is called Lagrangian relaxation. The regular $DP$ approach, where $DP[a][b]$ represents the length of the longest increasing subsequence that ends in the $a$'th number and restarts at ...
Antti Röyskö's user avatar
5 votes

How can you compute the expected edit distance in $O(2^{3n/2})$ time?

Heh…for the record, all I said was that the time seems to scale as approximately $\tilde O(2^{1.5n})$. If this was anything more than a conjecture based on the timing data that I included in the post, ...
Anders Kaseorg's user avatar
5 votes

3xN tiling problem with blocks of size 3x1 or 2x2

Here is a systematic way to approach it. I suggest you define three values: $A_0(n)$ is the number of ways to tile a $3 \times n$ region. $A_1(n)$ is the number of ways to tile a $3 \times n$ ...
D.W.'s user avatar
  • 169k
5 votes
Accepted

Minimum number of intervals from a set to cover all intervals of another set

Of all intervals you need to cover, focus now on the one that ends earliest. This interval has to be covered, so take a covering interval that covers this interval, and that ends the latest. Pick that ...
Ainsley H.'s user avatar
  • 18.1k
4 votes

Dynamic programming: speed of top down vs bottom up approaches

Top down approach would be faster in cases where the no of entries that you have to compute are significantly less than than the total number of entries. Remember, recursive approach is slow than ...
shiwang's user avatar
  • 481
4 votes

Can there exist more than one optimal solution in a dynamic programming problem?

Yes, but it's implicit in the dynamic programming table. The dynamic programming table starts by filling out objective values for trivial sub-solutions, then combining them to obtain the optimum ...
Luke Mathieson's user avatar
4 votes

Find longest subsequence in array with given condition

In the following answer, I show how to solve in $O(n\log n)$ the following similar question: Given an array $A$ and a number $k$, find the longest contiguous subsequence in which any two elements $x,...
Yuval Filmus's user avatar
4 votes
Accepted

Calculate the number of trailing zeros in equation f(n) = f(n-1) * f(n-2) where f(0) and f(1) are any given arbitary numbers

Assuming f(0) and f(1) are integers, find which powers of two and of five are factors of f(0) and f(1). The powers in f(n) can be calculated like a Fibonacci sequence and the number of trailing zeroes ...
gnasher729's user avatar
  • 32.6k
4 votes
Accepted

How does the problem of "Scheduling to Minimize Lateness" exhibit optimal substructure?

Let us refine the "definition" of the optimal substructure property given in CLRS into two definitions. A problem exhibits strongly optimal substructure if every optimal solution to the ...
喜欢算法和数学's user avatar
4 votes
Accepted

Number of contiguous subsequences summing to a given target

Here is a simple randomized $O(n)$ time algorithm. We start by rephrasing your problem slightly. Suppose that the original array is $a_1,\ldots,a_n$. Form a new array $b_0,\ldots,b_n$ containing the ...
Yuval Filmus's user avatar
4 votes
Accepted

Maximum product of contiguous subsequence over $\mathbb{R}$

You can adopt the dynamic programming solution; the other solution looks harder to adapt. Given an array $A_1,\ldots,A_n$, we let $P_i$ be the maximum positive product of a subsequence of $A_1,\ldots,...
Yuval Filmus's user avatar
4 votes
Accepted

Shortest path from source to all vertices, but with some wildcards

Take $K+1$ copies of your graph. For each edge $(x,y)$ and for each $i \in \{1,\ldots,K\}$, connect the $i$'th copy of $x$ to the $(i+1)$'th copy of $y$ with a zero weight edge. Also connect the $i$'...
Yuval Filmus's user avatar
4 votes
Accepted

Algorithms for 2-colouring a 2 x N matrix

One can view this problem as a dynamic programming problem with $3N$ subproblems. Let $RR(N)$ be the number of solutions for a $2\times N$ matrix where the first row is colored with red-red, $RB(N)$ ...
Tom van der Zanden's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible