dynamic-programming

Scala Memoization: How does this Scala memo work?

非 Y 不嫁゛ 提交于 2019-11-27 17:53:05
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 <% K, K, O](f: I => O) extends (I => O) { import collection.mutable.{Map => Dict} val cache = Dict

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

走远了吗. 提交于 2019-11-27 17:45:00
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 more efficient solution? An O(N) (number of elements) solution: A 1 1 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0

FSharp runs my algorithm slower than Python

被刻印的时光 ゝ 提交于 2019-11-27 17:15:16
Years ago, I solved a problem via dynamic programming: https://www.thanassis.space/fillupDVD.html The solution was coded in Python. As part of expanding my horizons, I recently started learning OCaml/F#. What better way to test the waters, than by doing a direct port of the imperative code I wrote in Python to F# - and start from there, moving in steps towards a functional programming solution. The results of this first, direct port... are disconcerting: Under Python: bash$ time python fitToSize.py .... real 0m1.482s user 0m1.413s sys 0m0.067s Under FSharp: bash$ time mono ./fitToSize.exe ....

Throwing cats out of windows

删除回忆录丶 提交于 2019-11-27 16:35:32
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 the maximal safe floor. But what if you have more than one cat? You can now try some sort of logarithmic

Explain this dynamic programming climbing n-stair code

江枫思渺然 提交于 2019-11-27 16:07:40
问题 Problem is "You are climbing a stair case. Each time you can either make 1 step or 2 steps. The staircase has n steps. In how many distinct ways can you climb the staircase?" Following is the code solution for this problem but I am having trouble understanding it. Can anybody explain me int stairs(int n) { if (n == 0) return 0; int a = 1; int b = 1; for (int i = 1; i < n; i++) { int c = a; a = b; b += c; } return b; } Thanks, 回答1: Well, first you need to understand the recursive formula, and

Longest Common Subsequence for Multiple Sequences

血红的双手。 提交于 2019-11-27 13:21:03
问题 I have done a bunch of research for finding the longest for M = 2 sequences, but I am trying to figure out how to do it for M ≥ 2 sequences I am being given N and M: M sequences, with N unique elements. N is the set of {1 - N}. I have thought about the dynamic programming approach, but I am still confused as to how to actually incorporate it. Example input 5 3 5 3 4 1 2 2 5 4 3 1 5 2 3 1 4 The max sequence here can be seen to be 5 3 1 Expected output Length = 3 回答1: A simple idea. For each

Algorithm to Divide a list of numbers into 2 equal sum lists

為{幸葍}努か 提交于 2019-11-27 11:08:44
There is a list of numbers. The list is to be divided into 2 equal sized lists, with a minimal difference in sum. The sums have to be printed. #Example: >>>que = [2,3,10,5,8,9,7,3,5,2] >>>make_teams(que) 27 27 Is there an error in the following code algorithm for some case? How do I optimize and/or pythonize this? def make_teams(que): que.sort() if len(que)%2: que.insert(0,0) t1,t2 = [],[] while que: val = (que.pop(), que.pop()) if sum(t1)>sum(t2): t2.append(val[0]) t1.append(val[1]) else: t1.append(val[0]) t2.append(val[1]) print min(sum(t1),sum(t2)), max(sum(t1),sum(t2)), "\n" Question is

Dynamic programming: Find longest subsequence that is zig zag

☆樱花仙子☆ 提交于 2019-11-27 09:36:12
问题 Can anyone please help me understand the core logic behind the solution to a problem mentioned at http://www.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493 A zig zag sequence is one that alternately increases and decreases. So, 1 3 2 is zig zag, but 1 2 3 is not. Any sequence of one or two elements is zig zag. We need to find the longest zig zag subsequence in a given sequence. Subsequence means that it is not necessary for elements to be contiguous, like in the longest increasing

How is dynamic programming different from greedy algorithms?

你。 提交于 2019-11-27 09:35:01
问题 In the book I am using Introduction to the Design & Analysis of Algorithms, dynamic programming is said to focus on the Principle of Optimality , "An optimal solution to any instance of an optimization problem is composed of optimal solutions to its subinstances". Whereas, the greedy technique focuses on expanding partially constructed solutions until you arrive at a solution for a complete problem. It is then said, it must be "the best local choice among all feasible choices available on

Largest rectangular sub matrix with the same number

橙三吉。 提交于 2019-11-27 09:33:21
问题 I am trying to come up with a dynamic programming algorithm that finds the largest sub matrix within a matrix that consists of the same number: example: {5 5 8} {5 5 7} {3 4 1} Answer : 4 elements due to the matrix 5 5 5 5 回答1: This is a question I already answered here (and here, modified version). In both cases the algorithm was applied to binary case (zeros and ones), but the modification for arbitrary numbers is quite easy (but sorry, I keep the images for the binary version of the