dynamic-programming

longest palindromic substring recursive solution

橙三吉。 提交于 2019-12-01 03:14:02
I am aware of solutions that uses the bottom up dynamic programing approach to solve this problem in O(n^2). I am specifically looking for a top down dp approach. Is it possible to achieve longest palindromic substring using a recursive solution? Here is what I have tried but it fails for certain cases, but I feel I am almost on the right track. #include <iostream> #include <string> using namespace std; string S; int dp[55][55]; int solve(int x,int y,int val) { if(x>y)return val; int &ret = dp[x][y]; if(ret!=0){ret = val + ret;return ret;} //cout<<"x: "<<x<<" y: "<<y<<" val: "<<val<<endl; if(S

Dynamic Programming - Word Break

自闭症网瘾萝莉.ら 提交于 2019-12-01 02:09:36
I am trying to solve this Problem.The question is as follows Given an input string and a dictionary of words, find out if the input string can be segmented into a space-separated sequence of dictionary words. Dictionary is an array of strings. My Approach is the following recursive fn with storing of the results of recursive calls. The output is fine but I see that the stored result is never used. My solution is hopefully correct as it passed the test cases.But I would be great if I know whether DP is used. The code is: #include <iostream> #include <string.h> using namespace std; int r[100]

Dynamic Programming Solution for Activity-selection

…衆ロ難τιáo~ 提交于 2019-12-01 01:22:56
In 16.1 An activity-selection problem of Introduction to Algorithm , the dynamic programming solution for this problem was given as c[i, j] = 0 if S(i, j) is empty c[i, j] = max { c[i, k] + c[k, j] + 1 } if S(i, j) is not empty where S(i, j) denotes the set of activities that start after activity a(i) finishes and that finish before activity a(j) starts, and c[i, j] denotes the size of an optimal solution for the set S(i, j) However, I am thinking of another simpler solution c[i] = max { c[i - 1], c[f(i)] + 1 } where f(i) gives the activity that is compatible with a(i) and has the max finish

Maximum profit that can be obtained - Buying and Selling

允我心安 提交于 2019-11-30 20:11:47
问题 Say we have an accurate prediction of Messi’s prices for a period of N days. The prediction is given as a list in which p_i represents the player’s price in day i . Bob plans to make multiple, successive transactions during this period, but CAN’T have more than one Messi at a time, and therefore needs to sell him before buying him again. Bob starts out with a limited budget B and can’t buy a Messi which costs more than he can afford. Of course, Bob can add to his budget any profit he gets

Finding an optimal solution that minimizes a constraint?

只谈情不闲聊 提交于 2019-11-30 20:11:15
Let us call this problem the Slinger-Bird problem (actually the Slinger is analogous to a server and the bird to a request but I was having a nervous breakdown thinking about it so I changed them hoping to get a different perspective!). There are S stone throwers (slingers) and B birds. The slingers are not within the range of each other. Slinging once can kill all birds within the sight of a slinger and will consume one shot and one time unit I am trying to figure out the optimal solution that minimizes the time and the number of shots it takes to kill the birds given a particular arrival

Dynamic Programming Solution for Activity-selection

隐身守侯 提交于 2019-11-30 19:11:43
问题 In 16.1 An activity-selection problem of Introduction to Algorithm , the dynamic programming solution for this problem was given as c[i, j] = 0 if S(i, j) is empty c[i, j] = max { c[i, k] + c[k, j] + 1 } if S(i, j) is not empty where S(i, j) denotes the set of activities that start after activity a(i) finishes and that finish before activity a(j) starts, and c[i, j] denotes the size of an optimal solution for the set S(i, j) However, I am thinking of another simpler solution c[i] = max { c[i

Algorithm to bracket an expression in order to maximize its value

时间秒杀一切 提交于 2019-11-30 19:06:21
I found this while looking up problems on dynamic programming. You are given an unparanthesized expression of the form V0 O0 V1 O1 .... Vn-1 We have to put brackets at places which maximizes the value of the entire expression. V's are the operands and O's are the operators. In the first version of problem operators can be * and + and operands are positive. Second version of problem is completely general. For the first version i came up with DP solution . Solution - A[] = operands array B[] - operators array T(A[i,j]) - max value of expression T(A[0,n-1]) = max over all i {(T(A[0,i]) Oi T(A[i+1

An interesting graph task

给你一囗甜甜゛ 提交于 2019-11-30 18:32:36
问题 There is a tree with n vertices. We are asked to calculate minimum size of a multiset S such for each edge (u,v) in the tree at least one of the following holds: u &in; S v &in; S there are at least two vertices in S, each of which is adjacent to u or v. Since S is a multiset, a vertex may be in S multiple times. My hunch is the following. First of all, we take into consideration the following fact: in optimal solution each vertex is in S at most twice. So we can traverse tree in post-order

Sum of products of elements of all subarrays of length k

隐身守侯 提交于 2019-11-30 15:48:37
问题 An array of length n is given. Find the sum of products of elements of the sub-array. Explanation Array A = [2, 3, 4] of length 3 . Sub-array of length 2 = [2,3], [3,4], [2,4] Product of elements in [2, 3] = 6 Product of elements in [3, 4] = 12 Product of elements in [2, 4] = 8 Sum for subarray of length 2 = 6+12+8 = 26 Similarly, for length 3 , Sum = 24 As, products can be larger for higher lengths of sub-arrays calculate in modulo 1000000007 . What is an efficient way for finding these sums

Algorithm for Human Towering

ぃ、小莉子 提交于 2019-11-30 15:38:38
问题 In Cracking the Coding Interview, Fourth Edition, there is such a problem: A circus is designing a tower routine consisting of people standing atop one anoth- er’s shoulders For practical and aesthetic reasons, each person must be both shorter and lighter than the person below him or her Given the heights and weights of each person in the circus, write a method to compute the largest possible number of people in such a tower. EXAMPLE: Input (ht, wt): (65, 100) (70, 150) (56, 90) (75, 190) (60