dynamic-programming

Dynamic programming sum

丶灬走出姿态 提交于 2019-11-29 07:42:30
How would you use dynamic programming to find the list of positive integers in an array whose sum is closest to but not equal to some positive integer K? I'm a little stuck thinking about this. The usual phrasing for this is that you're looking for the value closest to, but not exceeding K. If you mean "less than K", it just means that your value of K is one greater than the usual. If you truly mean just "not equal to K", then you'd basically run through the algorithm twice, once finding the largest sum less than K, then again finding the smallest sum greater than K, then picking the one of

Finding minimal absolute sum of a subarray

心不动则不痛 提交于 2019-11-29 06:27:12
There's an array A containing (positive and negative) integers. Find a (contiguous) subarray whose elements' absolute sum is minimal, e.g.: A = [2, -4, 6, -3, 9] |(−4) + 6 + (−3)| = 1 <- minimal absolute sum I've started by implementing a brute-force algorithm which was O(N^2) or O(N^3) , though it produced correct results. But the task specifies: complexity: - expected worst-case time complexity is O(N*log(N)) - expected worst-case space complexity is O(N) After some searching I thought that maybe Kadane's algorithm can be modified to fit this problem but I failed to do it. My question is -

Dynamic Programming and Knapsack Application

删除回忆录丶 提交于 2019-11-29 03:18:42
问题 Im studying dynamic programming and am looking to solve the following problem, which can be found here http://www.cs.berkeley.edu/~vazirani/algorithms/chap6.pdf: You are given a rectangular piece of cloth with dimensions X by Y, where X and Y are positive integers, and a list of n products that can be made using the cloth. For each product i in [1,n] you know that a rectangle of cloth of dimensions ai by bi is needed and that the final selling price of the product is ci. Assume that ai, bi,

How to find the minimum number of operation(s) to make the string balanced?

淺唱寂寞╮ 提交于 2019-11-29 03:13:06
问题 From Codechef: A string is considered balanced if and only if all the characters occur in it equal number of times. You are given a string S ; this string may only contain uppercase English letters. You may perform the following operation any number of times (including zero): Choose one letter in S and replace it by another uppercase English letter. Note that even if the replaced letter occurs in S multiple times, only the chosen occurrence of this letter is replaced. Find the minimum number

Optimal filling of grid figure with squares

夙愿已清 提交于 2019-11-29 02:13:05
recently I have designed a puzzle for children to solve. However I would like to now the optimal solution. The problem is as follows: You have this figure made up off small squares You have to fill it in with larger squares and it is scored with the following table: | Square Size | 1x1 | 2x2 | 3x3 | 4x4 | 5x5 | 6x6 | 7x7 | 8x8 | |-------------|-----|-----|-----|-----|-----|-----|-----|-----| | Points | 0 | 4 | 10 | 20 | 35 | 60 | 84 | 120 | There are simply to many possible solutions to check them all. Some other people suggested dynamic programming, but I don't know how to divide the figure

Explain this dynamic programming climbing n-stair code

北城余情 提交于 2019-11-29 01:36:36
Problem is "You are climbing a stair case. Each time you can either make 1 step or 2 steps. The staircase has n steps. In how many distinct ways can you climb the staircase?" Following is the code solution for this problem but I am having trouble understanding it. Can anybody explain me int stairs(int n) { if (n == 0) return 0; int a = 1; int b = 1; for (int i = 1; i < n; i++) { int c = a; a = b; b += c; } return b; } Thanks, Well, first you need to understand the recursive formula, and how we derived the iterative one from it. The recursive formula is: f(n) = f(n-1) + f(n-2) f(0) = f(1) = 1 (

How to compute optimal paths for traveling salesman bitonic tour?

假如想象 提交于 2019-11-29 01:11:28
问题 UPDATED After more reading, the solution can be given with the following recurrence relation: (a) When i = 1 and j = 2, l(i; j) = dist(pi; pj ) (b) When i < j - 1; l(i; j) = l(i; j - 1) + dist(pj-1; pj) (c) When i = j - 1 and j > 2, min 1<=k<i (l(k; i) + dist(pk; pj )) This is now starting to make sense, except for part C. How would I go about determining the minimum value k? I suppose it means you can iterate through all possible k values and just store the minimum result of ( l(k,i) + dist

How can I compute the number of characters required to turn a string into a palindrome?

六眼飞鱼酱① 提交于 2019-11-28 23:15:41
I recently found a contest problem that asks you to compute the minimum number of characters that must be inserted (anywhere) in a string to turn it into a palindrome. For example, given the string: "abcbd" we can turn it into a palindrome by inserting just two characters: one after "a" and another after "d": "a d bcbd a ". This seems to be a generalization of a similar problem that asks for the same thing, except characters can only be added at the end - this has a pretty simple solution in O(N) using hash tables. I have been trying to modify the Levenshtein distance algorithm to solve this

Square Subsequence

前提是你 提交于 2019-11-28 23:03:57
问题 A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Given a string, how many subsequences of the string are square strings? A subsequence of a string can be obtained by deleting zero or more characters from it, and maintaining the relative order of the remaining characters.The subsequence need not be unique. eg string 'aaa' will have 3 square subsequences 回答1:

Longest Common Subsequence for Multiple Sequences

走远了吗. 提交于 2019-11-28 20:45:12
I have done a bunch of research for finding the longest for M = 2 sequences, but I am trying to figure out how to do it for M ≥ 2 sequences I am being given N and M: M sequences, with N unique elements. N is the set of {1 - N}. I have thought about the dynamic programming approach, but I am still confused as to how to actually incorporate it. Example input 5 3 5 3 4 1 2 2 5 4 3 1 5 2 3 1 4 The max sequence here can be seen to be 5 3 1 Expected output Length = 3 A simple idea. For each number i between 1 and N , calculate the longest subsequence where the last number is i . (Let's call it a[i]