dynamic-programming

Lazily Tying the Knot for 1 Dimensional Dynamic Programming

谁说胖子不能爱 提交于 2019-11-30 11:19:57
Several years ago I took an algorithms course where we were giving the following problem (or one like it): There is a building of n floors with an elevator that can only go up 2 floors at a time and down 3 floors at a time. Using dynamic programming write a function that will compute the number of steps it takes the elevator to get from floor i to floor j . This is obviously easy using a stateful approach, you create an array n elements long and fill it up with the values. You could even use a technically non-stateful approach that involves accumulating a result as recursively passing it

Maximum possible number of rectangles that can be crossed with a single straight line

混江龙づ霸主 提交于 2019-11-30 10:58:41
问题 I found this challenge problem which states the following : Suppose that there are n rectangles on the XY plane. Write a program to calculate the maximum possible number of rectangles that can be crossed with a single straight line drawn on this plane. I have been brainstorming for quite a time but couldn't find any solution. Maybe at some stage, we use dynamic programming steps but couldn't figure out how to start. 回答1: Here is a sketch of an O(n^2 log n) solution. First, the preliminaries

Dynamic Programming Solution for a Variant of Coin Exchange

限于喜欢 提交于 2019-11-30 10:47:56
I am practicing Dynamic Programming. I am focusing on the following variant of the coin exchange problem: Let S = [1, 2, 6, 12, 24, 48, 60] be a constant set of integer coin denominations. Let n be a positive integer amount of money attainable via coins in S . Consider two persons A and B . In how many different ways can I split n among persons A and B so that each person gets the same amount of coins (disregarding the actual amount of money each gets)? Example n = 6 can be split into 4 different ways per person: Person A gets {2, 2} and person B gets {1, 1}. Person A gets {2, 1} and person B

What is the dynamic programming algorithm for finding a Hamiltonian cycle in a graph?

不羁岁月 提交于 2019-11-30 10:39:52
问题 What is dynamic programming algorithm for finding a Hamiltonian cycle in an undirected graph? I have seen somewhere that there exists an algorithm with O(n.2^n) time complexity. 回答1: There is indeed an O(n2 n ) dynamic-programming algorithm for finding Hamiltonian cycles. The idea, which is a general one that can reduce many O(n!) backtracking approaches to O(n 2 2 n ) or O(n2 n ) (at the cost of using more memory), is to consider subproblems that are sets with specified "endpoints" . Here,

Longest common subsequence of 3+ strings

依然范特西╮ 提交于 2019-11-30 10:32:12
问题 I am trying to find the longest common subsequence of 3 or more strings. The Wikipedia article has a great description of how to do this for 2 strings, but I'm a little unsure of how to extend this to 3 or more strings. There are plenty of libraries for finding the LCS of 2 strings, so I'd like to use one of them if possible. If I have 3 strings A, B and C, is it valid to find the LCS of A and B as X, and then find the LCS of X and C, or is this the wrong way to do it? I've implemented it in

Bytelandian Gold Coin , Dynamic programming , explanation?

*爱你&永不变心* 提交于 2019-11-30 10:16:02
It's a bit immature, but I have to ask, The Bytelandian Gold coin problem mentioned here - http://www.codechef.com/problems/COINS/ , is said to be typical DP problem,even though I have read basics of DP & recursion, but I am finding hard to understand its solution, # include <stdio.h> # include <stdlib.h> long unsigned int costArray[30][19]; unsigned int amount; unsigned int currentValue(short int factor2,short int factor3) { int j; unsigned int current = amount >> factor2; for(j=0;j<factor3;j++) current /= 3; return current; } long unsigned int findOptimalAmount(short int factor2,short int

Find subset of size k such that the minimum distance between values is maximum

只谈情不闲聊 提交于 2019-11-30 10:14:27
Suppose i have an array which contain n integers . How to find subset of size k such that the minimum distance between all pairs of integers in the subset is maximized , i mean they are at farthest distance . example : array a[]={1,2,6,7,10} and k=3 , subset = {1,6,10} , the minimum distance is 4 between 10 and 6 . Wrong subsets : {1,7,10} , minimum distance is 3 {1,2,6} , minimum distance is 1 I came up with a solution : 1) sort array 2) select a[0] , now find ceil(a[0]+ x ) = Y in array ....and then ceil(Y+ x ) and so on k-1 times , also kth element will be a[n-1] To find x : dp[i,j] be the

Minimum no of changes required to make array strictly increasing

房东的猫 提交于 2019-11-30 10:00:35
I have a problem in which we have an array of positive numbers and we have to make it strictly increasing by making zero or more changes to the array elements. We are asked the minimum number of changes required to make the array strictly increasing. Example if array is 1 2 9 10 3 15 so ans=1 if change 3 to some number between 12 to 14. if 1 2 2 2 3 4 5 ans=5 since changing 2 to 3 then 2 to 4 then 3 to 5 then 4 to 6 then 5 to 7 Constraints: Number of elements in array <= 10^6 Each element <= 10^9 Note: This is not a homework, it was asked in my interview and I was unable to give an algorithm

Maximum product prefix string

大城市里の小女人 提交于 2019-11-30 09:54:40
The following is a demo question from a coding interview site called codility: A prefix of a string S is any leading contiguous part of S. For example, "c" and "cod" are prefixes of the string "codility". For simplicity, we require prefixes to be non-empty. The product of prefix P of string S is the number of occurrences of P multiplied by the length of P. More precisely, if prefix P consists of K characters and P occurs exactly T times in S, then the product equals K * T. For example, S = "abababa" has the following prefixes: "a", whose product equals 1 * 4 = 4, "ab", whose product equals 2 *

Counting ways to climb n steps with 1, 2, or 3 steps taken

烈酒焚心 提交于 2019-11-30 09:47:23
问题 In a book I encountered following question: Given N step stair, in how many number of ways can you climb if you use either 1, 2 or 3 steps at a time? Following is the code that book has given: int countWays(int n){ if(n<0) return 0; if(n == 0) return 1; else return countWays(n-1) + countWays(n-2) + countWays(n-3); } I have the following concerns in understanding this code: I do not understand why 1 is being returned for n=0. If there are 0 steps then obviously we do not have to climb any and