dynamic-programming

Find maximum length of good path in a grid

£可爱£侵袭症+ 提交于 2019-12-19 07:09:12
问题 Given is a N*N grid.Now we need to find a good path of maximum length , where good path is defined as follow : Good path always start from a cell marked as 0 We are only allowed to move Left,Right,Up Or Down If the value of ith cell is say A, then value of next cell in the path must be A+1. Now given these few conditions, I need to find out the length of maximum path that can be made. Also I need to count such paths that are of maximum length. Example : Let N=3 and we have 3*3 matrix as

Longest Common Subsequence

大城市里の小女人 提交于 2019-12-18 15:55:18
问题 Consider 2 sequences X[1..m] and Y[1..n]. The memoization algorithm would compute the LCS in time O(m*n). Is there any better algorithm to find out LCS wrt time? I guess memoization done diagonally can give us O(min(m,n)) time complexity. 回答1: Gene Myers in 1986 came up with a very nice algorithm for this, described here: An O(ND) Difference Algorithm and Its Variations. This algorithm takes time proportional to the edit distance between sequences, so it is much faster when the difference is

Convert string to palindrome string with minimum insertions

蓝咒 提交于 2019-12-18 13:09:51
问题 In order to find the minimal number of insertions required to convert a given string(s) to palindrome I find the longest common subsequence of the string(lcs_string) and its reverse. Therefore the number of insertions to be made is length(s) - length(lcs_string) What method should be employed to find the equivalent palindrome string on knowing the number of insertions to be made? For example : 1) azbzczdzez Number of insertions required : 5 Palindrome string : azbzcezdzeczbza Although

0/1 Knapsack Dynamic Programming Optimazion, from 2D matrix to 1D matrix

有些话、适合烂在心里 提交于 2019-12-18 12:06:00
问题 I need some clarification from wikipedia: Knapsack, on the part This solution will therefore run in O(nW) time and O(nW) space. Additionally, if we use only a 1-dimensional array m[W] to store the current optimal values and pass over this array i+1 times, rewriting from m[W] to m[1] every time, we get the same result for only O(W) space. I am having trouble understanding how to turn a 2D matrix into a 1D matrix to save space. In addition, to what does rewriting from m[W] to m[1] every time

Nth Fibonacci number for n as big as 10^19?

孤者浪人 提交于 2019-12-18 12:02:37
问题 I am trying to make a program to find the nth Fibonacci number for 1 < n < 10^19. Here is my code using dynamic programming. memo = {} def fib(n): if n in memo: return memo[n] if n <= 2: f = 1 else: f = fib(n-1) + fib(n-2) memo[n]=f return f print fib(input()) % 1000000007 My code does not seem to work for large numbers. I get invalid response error. Any suggestions? 回答1: Getting the Nth fibonacci number when N is 10^19 is not goign to work if you do it the naive way (at least i would guess

Nth Fibonacci number for n as big as 10^19?

大城市里の小女人 提交于 2019-12-18 12:02:23
问题 I am trying to make a program to find the nth Fibonacci number for 1 < n < 10^19. Here is my code using dynamic programming. memo = {} def fib(n): if n in memo: return memo[n] if n <= 2: f = 1 else: f = fib(n-1) + fib(n-2) memo[n]=f return f print fib(input()) % 1000000007 My code does not seem to work for large numbers. I get invalid response error. Any suggestions? 回答1: Getting the Nth fibonacci number when N is 10^19 is not goign to work if you do it the naive way (at least i would guess

Generalised Two-Egg Puzzle

只愿长相守 提交于 2019-12-18 11:32:40
问题 Here is the Problem Description : Suppose that we wish to know which stories in a N-story building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions: An egg that survives a fall can be used again. A broken egg must be discarded. The effect of a fall is the same for all eggs. If an egg breaks when dropped, then it would break if dropped from a higher window. If an egg survives a fall then it would survive a shorter fall. It is not ruled

Dynamic programming in the functional paradigm

允我心安 提交于 2019-12-18 10:13:24
问题 I'm looking at Problem thirty one on Project Euler, which asks, how many different ways are there of making £2 using any number of coins of 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). There are recursive solutions, such as this one in Scala (credit to Pavel Fatin) def f(ms: List[Int], n: Int): Int = ms match { case h :: t => if (h > n) 0 else if (n == h) 1 else f(ms, n - h) + f(t, n) case _ => 0 } val r = f(List(1, 2, 5, 10, 20, 50, 100, 200), 200) and although it runs fast enough, it

Dynamic programming for primitive calculator

寵の児 提交于 2019-12-18 09:08:36
问题 I'm dealing with the problem, that is pretty similar to change coins problem. I need to implement a simple calculator, that can perform the following three operations with the current number x: multiply x by 2, multiply x by 3, or add 1 to x. Goal is given a positive integer n, find the minimum number of operations needed to obtain the number n starting from the number 1. I made a greedy approach to that, bur it shows incorrect results import sys def optimal_sequence(n): sequence = [] while n

Algorithm to find maximum coverage of non-overlapping sequences. (I.e., the Weighted Interval Scheduling Prob.)

◇◆丶佛笑我妖孽 提交于 2019-12-18 07:02:46
问题 I have a question that is very similar to algorithm to find longest non-overlapping sequences. The only difference to the linked question is that instead of finding the set of non-overlapping tuples that represent the longest sequence , I need to find the set of non-overlapping tuples that represent the maximum coverage , by which I mean the sum of the tuple lengths is maximum (a tuple length being last - first + 1 given the definition of tuple in the next sentence). I represent my tuples