dynamic-programming

Copying Books UVa Online Judge Dynamic Programing Solution

こ雲淡風輕ζ 提交于 2019-12-05 01:10:30
问题 I can solve Copying Books Problem using binary search method as it is easy to implement. But I have just started solving Dynamic Programing problems and I wanted to know Dynamic Programing solution for the problem Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its copy. One of the most famous scribers lived in the 15th

Sub-sequence of Vowels

廉价感情. 提交于 2019-12-04 22:41:35
问题 I was practicing for an interview and came across this question on a website: A magical sub-sequence of a string S is a sub-sequence of S that contains all five vowels in order. Find the length of largest magical sub-sequence of a string S . For example, if S = aeeiooua , then aeiou and aeeioou are magical sub-sequences but aeio and aeeioua are not. I am a beginner in dynamic programming and am finding it hard to come up with a recursive formula for this. 回答1: I did it with an iterative

Recursive change-making algorithm

拟墨画扇 提交于 2019-12-04 22:32:02
问题 Given a target amount and a list of coin denominations, my code is supposed to find the fewest coins needed to reach the target amount. Examples: C(78, [1, 5, 10, 25, 50]) = 6 we can make 78 from 3x 25 + 3x 1 , so 6 coins are required C(48, [1, 7, 24, 42]) = 2 48 = 2x 24 , so 2 coins are sufficient C(35, [1, 3, 16, 30, 50]) = 3 we can make 35 from 2x 16 + 1x 3 , so 3 coins suffice I made the code with for loops, but how do I make it recursive? def C(i, coins, cdict = None): if cdict == None:

Broken Calculator

て烟熏妆下的殇ゞ 提交于 2019-12-04 21:54:07
Problem Statement: There is a broken calculator. Only a few of the digits [ 0 to 9 ] and operators [ +, -, *, / ] are working. A req no. needs to be formed using the working digits and the operators. Each press on the keyboard is called an operation. = operator is always working and is used when the req no. is formed using operators. -1 needs to be printed in case the req no. cannot be formed using the digits and the operators provided OR exceeds the max no. of operations allowed. At no point in time during the calculation of the result, the no. should become negative or exceed 999 [ 0 <=

Data.MemoCombinators, where can I find examples?

梦想与她 提交于 2019-12-04 21:17:42
问题 This package has some functions to turn recursive functions into dynamic programming recursive functions, for better performance: http://hackage.haskell.org/packages/archive/data-memocombinators/0.3/doc/html/Data-MemoCombinators.html#t:Memo Unfortunately, they only have an example for the simplest type of function, and there are no examples on how to use a function of 2 variables. Where can I find an example of how to, for example, turn [Int] -> Int -> Int function into a dynamic-programming

Find all combinations of a given set of integers summing up to a given sum

送分小仙女□ 提交于 2019-12-04 20:48:44
I am looking for an answer to the following problem. Given a set of integers (no duplicates) and a sum, find all possible combinations of the set's elements summing up to the sum. Solutions order does not matter (solutions {2, 2, 3} and {3, 2 ,2} are equal). Please note that the final combination does not need to be a set, as it can contain duplicates. Example: Set {2,3,5} Sum 10 Result: {2, 2, 2, 2, 2}, {2, 2, 3, 3}, {2, 3, 5}, {5, 5} I've looked at Subset Sum problem as well as Coin Change problem, but couldn't adapt them to suit my needs. I am not really familiar with dynamic programming,

Given a set of ranges S, and an overlapping range R, find the smallest subset in S that encompases R

廉价感情. 提交于 2019-12-04 20:27:17
问题 The following is a practice interview question that was given to me by someone, and I'm not sure what the best solution to this is: Given a set of ranges: (e.g. S = {(1, 4), (30, 40), (20, 91) ,(8, 10), (6, 7), (3, 9), (9, 12), (11, 14)} . And given a target range R (e.g. R = (3, 13) - meaning the range going from 3 to 13). Write an algorithm to find the smallest set of ranges that covers your target range. All of the ranges in the set must overlap in order to be considered as spanning the

Maximum sum of absolute differences dynamic programming

爷,独闯天下 提交于 2019-12-04 20:17:43
We have an array of N elements. I have to choose K elements from this array(you can't take an element with index that is greater than the index of the element of the next one) in such a way that the sum abs(a1-a0)+abs(a2-a1)+...+(ak-ak-1) is maximized. My idea is 2d DP and goes like this: The solution that involves taking the n -th element while having taken k elements is the maximum of the solutions where element with index j smaller than i is the last one, j from k-1 , j<i plus taking the abs(numbers[n]-numbers[j]) . Here is my code. Where is my mistake? import java.io.BufferedReader; import

Improve C++ Fibonacci series

江枫思渺然 提交于 2019-12-04 19:54:16
I know that: int fib(int n) { if (n == 0 || n == 1) return 1; return fib(n − 1)+ fib(n − 2); } when n=5,fib(5) evaluates as: fib(5) fib(4) + fib(3) (fib(3) + fib(2)) + (fib(2) + fib(1)) ((fib(2) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) + fib(1)) (((fib(1) + fib(0)) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) + fib(1)) Notice that each base element being used for several times, is there a way to use map to store the previous value and simply do fib(n − 1) + fib(n − 2)? In C++, the two solutions at your disposal that would save you time are the dynamic programming approach and

Minimum tip to be paid for bill amount B with two kind of coins (x,y) only

旧巷老猫 提交于 2019-12-04 19:21:37
I have two kind of coins (unlimited coins of each type). The values of these two coins are x and y . I have to pay a bill of amount B . What minimum amount i will need to pay as tip . tip can be any value >=0 The objective is to minimize the tip. I just was thinking about the Dynamic programming approach.Or any Faster method. Please help. function minTip(x,y,B){ if(z<=0) return -z; return minimum( minTip(x,y,B-x),minTip(x,y,B-y) ); } Can any one help with the DP approach.?? Paul Hankin You don't need DP to solve this. First, note that you may as well assume the coins are coprime. Because if