dynamic-programming

Python Coin Change Dynamic Programming

寵の児 提交于 2020-01-03 06:31:09
问题 I am currently trying to implement dynamic programming in Python, but I don't know how to setup the backtracking portion so that it does not repeat permutations. For example, an input would be (6, [1,5]) and the expected output should be 2 because there are 2 possible ways to arrange 1 and 5 so that their sum is equivalent to 6. Those combinations are {1,1,1,1,1,1} and {1,5} but the way my program currently works, it accounts for the combinations displayed above and the combination {5,1}.

Maximum Area of rectangle without any monsters

好久不见. 提交于 2020-01-03 05:52:07
问题 Given a rectangular grid of N*M (1-based indexing) in which their are k monsters on k different cells.Now we need to answer Q queries in which we will be given lowest row number(L) and highest row number(H) we need to tell maximum area of rectangle between those rows that don't have a monster.(Here area of rectangle means count of cells only) Example : Say we have a grid of 4 * 5 (mean n=4 and m=5) and monsters are located on 7(=k) cells which are (1,3) , (1,4) , (2,1) , (2,4) , (3,2) , (4,1)

Maximum Value taken by thief

血红的双手。 提交于 2020-01-03 05:13:30
问题 Consider we have a sacks of gold and thief wants to get the maximum gold. Thief can take the gold to get maximum by, 1) Taking the Gold from contiguous sacks. 2) Thief should take the same amount of gold from all sacks. N Sacks 1 <= N <= 1000 M quantity of Gold 0 <= M <= 100 Sample Input1: 3 0 5 4 4 4 Output: 16 Explanation: 4 is the minimum amount he can take from the sacks 3 to 6 to get the maximum value of 16. Sample Input2: 2 4 3 2 1 Output: 8 Explanation: 2 is the minimum amount he can

How can I solve this classical dynamic programming problem?

馋奶兔 提交于 2020-01-03 00:57:23
问题 There are N jewellery shop(s). Each jewellery shop has three kinds of coins - Gold, Platinum, and Diamond having worth value A, B, and C respectively. You decided to go to each of N jewellery shop and take coins from each of the shop. But to do so following conditions must satisfy - You can take at most 1 coin from an individual shop. You can take at most X coins of Gold type. You can take at most Y coins of Platinum type. You can take at most Z coins of Diamond type. You want to collect

confusion about rod cutting algorithm - dynamic programming

孤人 提交于 2020-01-02 20:08:15
问题 I recently saw a rod cutting problem, where B(i) = optimal price for cutting a rod of length i units and p(i) = price of a rod of length i units. The algorithm given is something like this: B(i) = max(1<=k<=i) {p(k) + B(i-k)} Shouldn't it be something like this: B(i) = max(1<=k<=floor(i/2)) {B(k) + B(i-k)} where B(1) = p(1); so that both parts 've the optimal cost instead of cost for a single rod for one part and optimal cost for the second part. for example: B(4) = max{ (B(1) + B(3)); (B(2)

dynamic programming : traversal of cities

徘徊边缘 提交于 2020-01-02 08:44:53
问题 I came across this question: There are two persons. There is an ordered sequence of n cities, and the distances between every pair of cities is given. You must partition the cities into two subsequences (not necessarily contiguous) such that person A visits all cities in the first subsequence (in order), person B visits all cities in the second subsequence (in order), and such that the sum of the total distances travelled by A and B is minimized. Assume that person A and person B start

Optimal substructure

六眼飞鱼酱① 提交于 2020-01-02 07:46:06
问题 I'm trying to get a fuller picture of the use of the optimal substructure property in dynamic programming, yet I've gone blind on why we have to prove that any optimal solution to the problem contains within it optimal solutions to the sub-problems. Wouldn't it be enough to show that some optimal solution to the problem has this property, and then use this to argue that since the solution built by our recursive algorithm is at least as good as an optimal solution, it will itself be optimal?

Dynamic programming: find the subset with product of all members is equals to given number

一笑奈何 提交于 2020-01-02 04:27:08
问题 I will find an algorithm for this problem. Input: array of n integers and number k We must find a set of numbers from array, that product of all this numbers in set equals to k is I think, I must use dynamic programming for this task. But I have no idea, how to use it. 回答1: This is similar to the subset sum problem, where you are required to find a subset whom sum is a value k . Since there is a solution to your problem (you have a subset S whom multiplication is k ) if and only if you have a

Clarification of Answer… find the max possible two equal sum in a SET

二次信任 提交于 2020-01-02 03:35:23
问题 I need a clarification of the answer of this question but I can not comment (not enough rep) so I ask a new question. Hope it is ok. The problem is this: Given an array, you have to find the max possible two equal sum, you can exclude elements. i.e 1,2,3,4,6 is given array we can have max two equal sum as 6+2 = 4+3+1 i.e 4,10,18, 22, we can get two equal sum as 18+4 = 22 what would be your approach to solve this problem apart from brute force to find all computation and checking two possible

Algorithm to get every possible subset of a list, in order of their product, without building and sorting the entire list (i.e Generators)

主宰稳场 提交于 2020-01-01 06:17:31
问题 Practically, I've got a set of objects with probabilities, and I want to look at each possible group of them, in order of how likely it is that they're all true assuming they're independent -- i.e. in descending order of the product of the elements of the subsets -- or in order of length if the probabilities are the same (so that (1, 0.5) comes after (0.5)). Example: If I have [ 1, 0.5, 0.1 ] I want [ (), (1), (0.5), (1, 0.5), (0.1), (1, 0.1), (0.5, 0.1), (1, 0.5, 0.1) ] In essence, this