dynamic-programming

Longest `subsequence` of balanced parentheses

本秂侑毒 提交于 2019-12-08 04:00:19
问题 I am trying to solve a variant of a problem I asked earlier: Given a string of parentheses (length <= 1,000,000) and a list of range queries, find the longest subsequence of balanced parentheses within each of the ranges for each of the <= 100,000 queries I found this other SO question that is similar but only has an O(N^3) algorithm. I believe that a DP solution of the form dp[i, j] = longest balanced subsequence in [i .. j] should work because once computed, this would enable to to answer

Is the Sieve of Eratosthenes an example of Dynamic Programming?

半腔热情 提交于 2019-12-08 03:53:08
问题 I'm a bit confused as to whether the Sieve of Eratosthenes (implemented with an array for all the numbers and a loop marking the composite numbers) is an example of Dynamic Programming? A couple of friends were telling me the way it's implemented is an example of Bottom Up DP, but I'm having trouble seeing it. Exactly what are the subproblems and how would you implement SoE with Top-Down / Recursion? Thanks guys. 回答1: Sure, we could think of the Sieve of Eratosthenes as an example of dynamic

Complex Combinatorial Conditions on Dynamic Programming

▼魔方 西西 提交于 2019-12-08 03:31:01
问题 I am exploring how a Dynamic Programming design approach relates to the underlying combinatorial properties of problems. For this, I am looking at the canonical instance of the coin change problem : Let S = [d_1, d_2, ..., d_m] and n > 0 be a requested amount. In how many ways can we add up to n using nothing but the elements in S ? If we follow a Dynamic Programming approach to design an algorithm for this problem that would allow for a solution with polynomial complexity, we would start by

Find the most efficient grouping of a series of intervals

99封情书 提交于 2019-12-08 03:00:00
问题 I have an application where I have a series of non overlapping fixed width intervals, each of which have a given key. Each interval has the same width, and there may be contiguous intervals. Essentially I want to group the intervals and keys in such a way that I minimize the amount of separate intervals. This could be done by merging contiguous intervals with the same key or looking for matching intervals and combining them into single intervals with multiple keys. My current algorithm tries

Calculating number of moves from top left corner to bottom right with move in any direction

狂风中的少年 提交于 2019-12-08 02:49:29
问题 I have a problem asked to me in an interview, this is a similar problem I found so I thought of asking here. The problem is There is a robot situated at (1,1) in a N X N grid, the robot can move in any direction left, right ,up and down. Also I have been given an integer k, which denotes the maximum steps in the path. I had to calculate the number of possible ways to move from (1,1) to (N,N) in k or less steps. I know how to solve simplified version of this problem, the one with moves

Sellers and Buyers

試著忘記壹切 提交于 2019-12-07 22:29:06
问题 This is a followup of this question. Lets say there are several sellers and buyers, each seller has an amount of products in stock, each buyer also has a number of products they want to get. Now you are asked to find a best strategy to satisfy as many buyers as possible. (Here satisfy does not mean giving each buyer as many products as possible, if the amount of products available to this buyer is smaller than his requirement, this transaction can not be done and this buyer is not satisfied.

Directed graph linear algorithm

本秂侑毒 提交于 2019-12-07 20:36:30
I would like to know the best way to calculate the length of the shortest path between vertex s and every other vertex of the graph in linear time using dynamic programming. The graph is weighted DAG. What you can hope for is an algorithm linear in the number of edges and vertices, i.e. O(|E| + |V|) , which also works correctly in presence of negative weights. This is done by first computing a topological order and then 'exploring' the graph in the order given by this topological order. Some notation: let's call d'(s,v) the shortest distance from s to v and d(u,v) the length/weight of the arc

Maximum profit by buying and selling a share exactly k times

倾然丶 夕夏残阳落幕 提交于 2019-12-07 15:15:24
问题 The cost of a bond on each day is given in array prices of length n , and I need to find the maximum profit that I can make by buying and selling in exactly k transactions (buying and selling, in that order. not in the same day. but I can sell and then buy in the same day). I tried (Python): prices = [3, 1, 10] n = len(prices) def aux(i, j): if j == n - 1 or i == 0: return 0 s = [(prices[j + t] - prices[j]) + aux(i - 1, j + t) for t in range(1, n - j)] return max(aux(i, j + 1), max(s)) if s

Dynamic programming algorithm for facility locations

强颜欢笑 提交于 2019-12-07 14:47:21
问题 There are n houses at locations a_1, a_2,..., a_n along a line. We want to set up porta potties along that same line so that every house is within distance R of at least one porta potty. These porta potties are restricted to the specified locations b_1, b_2,..., b_m. Let c_i be the cost of setting up a porta potty at location b_i. Find a dynamic programming algorithm that minimizes the total cost of setting up the porta potties. The algorithm should be able to detect if a solution does not

Knapsack with “at least X value” constraint

你。 提交于 2019-12-07 14:19:03
问题 How would you go about solving this variation of Knapsack? You have n objects (x1,...xn), each with cost ci and value vi (1<=i<=n) and an additional constraint X, which is a lower bound on the value of the items selected. Find a subset of x1,...,xn that minimizes the cost of the items with a value of at least X. I am trying to solve this through Dynamic Programming, and what I thought of was to modify the usual table used into K[n,c,X] where X would be the minimum value I need to reach, but