dynamic-programming

Good examples, articles, books for understanding dynamic programming [closed]

自古美人都是妖i 提交于 2019-11-26 23:44:30
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 8 years ago . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. I can't figure out the principles of dynamic programming and I really do want it. DP is very powerful, it can solve problems like this:

Can't access derived class method from pointer of type base class

戏子无情 提交于 2019-11-26 23:19:40
问题 I should specify that I'm a bit new to OOP. I'm tying to make a vector of type pointer to Person that has a method GetName() and access a method GetSpg() from my Player class that derives Person. I get an error "GetSpg() is not a member of Person". My question would be: is there any way to access both functions from the vector so that if it points to a Person to not show that method but if it is to do so? Here is my code: #ifndef _PERSON_H #define _PERSON_H #include <iostream> #include

Interpretation vs dynamic dispatch penalty in Python

試著忘記壹切 提交于 2019-11-26 23:12:34
问题 I watched Brandon Rhodes' talk about Cython - "The Day of the EXE Is Upon Us". Brandon mentions at 09:30 that for a specific short piece of code, skipping interpretation gave 40% speedup, while skipping the allocation and dispatch gave 574% speedup (10:10). My question is - how is this measured for a specific piece of code? Does one need to manually extract the underlying c commands and then somehow make the runtime run them? This is a very interesting observation, but how do I recreate the

Scala Memoization: How does this Scala memo work?

筅森魡賤 提交于 2019-11-26 19:12:40
问题 The following code is from Pathikrit's Dynamic Programming repository. I'm mystified by both its beauty and peculiarity. def subsetSum(s: List[Int], t: Int) = { type DP = Memo[(List[Int], Int), (Int, Int), Seq[Seq[Int]]] implicit def encode(key: (List[Int], Int)) = (key._1.length, key._2) lazy val f: DP = Memo { case (Nil, 0) => Seq(Nil) case (Nil, _) => Nil case (a :: as, x) => (f(as, x - a) map {_ :+ a}) ++ f(as, x) } f(s, t) } The type Memo is implemented in another file: case class Memo[I

Finding maximum size sub-matrix of all 1's in a matrix having 1's and 0's

一世执手 提交于 2019-11-26 19:09:44
问题 Suppose you are given an mXn bitmap, represented by an array M[1..m,1.. n] whose entries are all 0 or 1. A all-one block is a subarray of the form M[i .. i0, j .. j0] in which every bit is equal to 1. Describe and analyze an efficient algorithm to find an all-one block in M with maximum area I am trying to make a dynamic programming solution. But my recursive algorithm runs in O(n^n) time, and even after memoization I cannot think of bringing it down below O(n^4). Can someone help me find a

Why is the knapsack problem pseudo-polynomial?

吃可爱长大的小学妹 提交于 2019-11-26 18:48:00
问题 I know that Knapsack is NP-complete while it can be solved by DP. They say that the DP solution is pseudo-polynomial , since it is exponential in the "length of input" (i.e. the numbers of bits required to encode the input). Unfortunately I did not get it. Can anybody explain that pseudo-polynomial thing to me slowly ? 回答1: The running time is O(NW) for an unbounded knapsack problem with N items and knapsack of size W. W is not polynomial in the length of the input though, which is what makes

Find the number of occurrences of a subsequence in a string

a 夏天 提交于 2019-11-26 18:43:37
问题 For example, let the string be the first 10 digits of pi, 3141592653 , and the subsequence be 123 . Note that the sequence occurs twice: 3141592653 1 2 3 1 2 3 This was an interview question that I couldn't answer and I can't think of an efficient algorithm and it's bugging me. I feel like it should be possible to do with a simple regex, but ones like 1.*2.*3 don't return every subsequence. My naive implementation in Python (count the 3's for each 2 after each 1) has been running for an hour

Throwing cats out of windows

好久不见. 提交于 2019-11-26 18:42:47
问题 Imagine you're in a tall building with a cat. The cat can survive a fall out of a low story window, but will die if thrown from a high floor. How can you figure out the longest drop that the cat can survive, using the least number of attempts? Obviously, if you only have one cat, then you can only search linearly. First throw the cat from the first floor. If it survives, throw it from the second. Eventually, after being thrown from floor f, the cat will die. You then know that floor f-1 was

Getting the submatrix with maximum sum?

ぐ巨炮叔叔 提交于 2019-11-26 18:18:35
Input : A 2-dimensional array NxN - Matrix - with positive and negative elements. Output : A submatrix of any size such that its summation is the maximum among all possible submatrices. Requirement : Algorithm complexity to be of O(N^3) History: With the help of the Algorithmist, Larry and a modification of Kadane's Algorithm, i managed to solve the problem partly which is determining the summation only - below in Java. Thanks to Ernesto who managed to solve the rest of the problem which is determining the boundaries of the matrix i.e. top-left, bottom-right corners - below in Ruby. Ernesto

How to understand the dynamic programming solution in linear partitioning?

大城市里の小女人 提交于 2019-11-26 17:34:48
问题 I'm struggling to understand the dynamic programming solution to linear partitioning problem. I am reading the The Algorithm Design Manual and the problem is described in section 8.5. I've read the section countless times but I'm just not getting it. I think it's a poor explanation (the what I've read up to now has been much better), but I've not been able to understand the problem well enough to look for an alternative explanation. Links to better explanations welcome! I've found a page with