dynamic-programming

Given a string of numbers and a number of multiplication operators, what is the highest number one can calculate?

◇◆丶佛笑我妖孽 提交于 2019-12-02 16:57:11
This was an interview question I had and I was embarrassingly pretty stumped by it. Wanted to know if anyone could think up an answer to it and provide the big O notation for it. Question: Given a string of numbers and a number of multiplication operators, what is the highest number one can calculate? You must use all operators You cannot rearrange the string. You can only use the multiplication operators to calculate a number. E.g. String = "312" , 1 multiplication operator You can do 3*12 = 36 or 31*2= 62 . The latter obviously being the right answer. I am assuming here that the required

Discover long patterns

别来无恙 提交于 2019-12-02 14:14:36
Given a sorted list of numbers, I would like to find the longest subsequence where the differences between successive elements are geometrically increasing. So if the list is 1, 2, 3, 4, 7, 15, 27, 30, 31, 81 then the subsequence is 1, 3, 7, 15, 31 . Alternatively consider 1, 2, 5, 6, 11, 15, 23, 41, 47 which has subsequence 5, 11, 23, 47 with a = 3 and k = 2. Can this be solved in O( n 2 ) time? Where n is the length of the list. I am interested both in the general case where the progression of differences is ak , ak 2 , ak 3 , etc., where both a and k are integers, and in the special case

What's the difference between recursion, memoization & dynamic programming? [duplicate]

拟墨画扇 提交于 2019-12-02 13:57:52
Possible Duplicate: Dynamic programming and memoization: top-down vs bottom-up approaches I have gone through a lot of articles on this but can't seem to make sense of it. At times recursion and dynamic programming looks the same and at others memoization & dynamic programming look alike. Can someone explain to me what's the difference? P.S. It will also be helpful if you could point me to some code using the three approaches on the same problem. (e.g. the Fibonacci series problem, I think every article I read used recursion but referred to it as dynamic programming) Consider calculating the

Can not understand knapsack solutions

时光总嘲笑我的痴心妄想 提交于 2019-12-02 13:38:30
问题 In wikipedia the algorithm for Knapsack is as follows: for i from 1 to n do for j from 0 to W do if j >= w[i] then T[i, j] := max(T[i-1, j], T[i-1, j-w[i]] + v[i]) [18] else T[i, j] := T[i-1, j] end if end for end for And it is the same structures on all examples I found online. What I can not understand is how does this code take into account the fact that perhaps the max value comes from a smaller knapsack? E.g. if the knapsack capacity is 8 then perhaps max value comes from capacity 7 (8 -

Algorithm for 'good' number

回眸只為那壹抹淺笑 提交于 2019-12-02 12:58:33
A give number x is 'good' if the sum of any two consecutive digit of the number x are between k and 2k. I need to find an algorithm that for a given number k and a given number n, find how many 'good' n-digit numbers exist. I made an implementation for this in PHP, but the complexity is to big (i am searching for all those 'good' number and counting them, so the complexity is O(10^n)). <?php $n = 5; $k = 5; $min = $k*1; $max = $k*2; $counter = 0; for ($i = pow(10, $n-1); $i<pow(10,$n); $i++) { $number = $i; $prev = $number % 10; $number = $number / 10; while($number >= 10) { $crnt = $number %

Dynamic programming - Algorithm to repair text where is all punctuation missing

二次信任 提交于 2019-12-02 12:25:51
This is description of my problem: I was thinking about start from left and add one letter and if it's word then check rest if could be separated to words (call recursion function). If yes then I have result if not then I would add one more letter to first word and so on. But this would take first smaller words and I guess it's needed to start with algorithm which check first longer words and then smaller. So I was thinking about start with whole word and then recursively check left part without last letter and then right part. If it wouldn't return true then I moved to left with "cursor" and

all solutions to change making with dynamic programming

旧城冷巷雨未停 提交于 2019-12-02 07:46:12
问题 I was reviewing my handouts for our algorithm class and I started to think about this question: Given different types of coins with different values, find all coin configurations to add up to a certain sum without duplication. During class, we solved the problem to find the number of all possible ways for a sum and the least number of coins for a sum. However, we never tried to actually find the solutions. I was thinking about solving this problem with dynamic programming. I came with the

Can not understand knapsack solutions

邮差的信 提交于 2019-12-02 05:53:30
In wikipedia the algorithm for Knapsack is as follows: for i from 1 to n do for j from 0 to W do if j >= w[i] then T[i, j] := max(T[i-1, j], T[i-1, j-w[i]] + v[i]) [18] else T[i, j] := T[i-1, j] end if end for end for And it is the same structures on all examples I found online. What I can not understand is how does this code take into account the fact that perhaps the max value comes from a smaller knapsack? E.g. if the knapsack capacity is 8 then perhaps max value comes from capacity 7 (8 - 1). I could not find anywhere logic to consider that perhaps the max value comes from a smaller

Upper limits for fibonnacci

梦想与她 提交于 2019-12-01 22:43:12
I was reading about the DP version of fibonnaci. In Sedgewick I saw: int[] T = new int[47]; for storage of the previous calculations. Elsewhere I saw that the max input for fibonacci should be less than 92 . It is not clear to me how does these numbers come up? I understand that it has to do with overflow and size of int but I am not clear how we end up with these limits. Any help? Well, the fibonacci series grows (approximately) exponentially with a ratio of 1.618 (the golden ratio). If you take the log base 1.618 of Integer.MAX_VALUE it will therefore tell you approximately how many

How do you check if one array is a subsequence of another?

纵饮孤独 提交于 2019-12-01 20:43:29
I'm looking to explore different algorithms, both recursive and dynamic programming, that checks if one arrayA is a subsequence of arrayB. For example, arrayA = [1, 2, 3] arrayB = [5, 6, 1, 7, 2, 9, 3] thus, arrayA is indeed a subsequence of arrayB. I've tried a few different searches, but all I can seem to find is algorithms to compute the longest increasing subsequence. Since you must match all elements of arrayA to some elements of arrayB , you never need to backtrack . In other words, if there are two candidates in arrayB to match an element of arrayA , you can pick the earliest one, and