dynamic-programming

Dynamic Programming - making change

对着背影说爱祢 提交于 2019-11-27 06:54:58
问题 I'm having trouble figuring out my last section of code for a Dynamic Coin Changing Problem. I have included the code below. I can't figure out the last else . Should I just use the greedy algorithm at that point or can I calculate the answer from values already in the table? I've worked hard on trying to understand this problem and I think I'm pretty close. The method finds the minimum amount of coins needed to make a certain amout of change by creating a table and using the results that are

Is there a generic way to memoize in Scala?

你。 提交于 2019-11-27 06:29:36
I wanted to memoize this: def fib(n: Int) = if(n <= 1) 1 else fib(n-1) + fib(n-2) println(fib(100)) // times out So I wrote this and this surprisingly compiles and works (I am surprised because fib references itself in its declaration): case class Memo[A,B](f: A => B) extends (A => B) { private val cache = mutable.Map.empty[A, B] def apply(x: A) = cache getOrElseUpdate (x, f(x)) } val fib: Memo[Int, BigInt] = Memo { case 0 => 0 case 1 => 1 case n => fib(n-1) + fib(n-2) } println(fib(100)) // prints 100th fibonacci number instantly But when I try to declare fib inside of a def , I get a

How to understand the dynamic programming solution in linear partitioning?

烂漫一生 提交于 2019-11-27 06:12:50
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 text similar to the book (maybe from the first edition of the book): The Partition Problem . First

How to count integers between large A and B with a certain property?

倖福魔咒の 提交于 2019-11-27 05:51:07
In programming contests, the following pattern occurs in a lot of tasks: Given numbers A and B that are huge (maybe 20 decimal digits or more), determine the number of integers X with A ≤ X ≤ B that have a certain property P SPOJ has lots of tasks like these for practice. Where examples of interesting properties include: "the digit sum of X is 60" "X consists only of the digits 4 and 7" "X is palindromic", which means that the decimal representation of X equals its reverse (for example, X = 1234321) I know that if we define f(Y) to be the number of such integers X ≤ Y, then the answer to our

Maximum Contiguous Subsequence Sum of At Least Length L

半城伤御伤魂 提交于 2019-11-27 05:16:08
问题 So for the following array, where L = 3 -5 -1 2 -3 0 -3 3 The best possible sum of at least length 3 would be 0, where the subsequence is the last three elements (0, -3, 3) How can you calculate this sum for any array in faster than O(NL) (effectively O(N^2) if L==0) time? 回答1: I believe that you can do this in O(n) time regardless of the choice of by using a modified version of Kadane's algorithm. To see how this works, let's consider the case where L = 0. In that case, we want to find the

Maximizing profit for given stock quotes

筅森魡賤 提交于 2019-11-27 05:04:25
问题 I was asked this question while interviewing for a startup and saw this again in the recent contest at Code Sprint:systems **The question : You are given the stock prices for a set of days . Each day, you can either buy one unit of stock, sell any number of stock units you have already bought, or do nothing. What is the maximum profit you can obtain by planning your trading strategy optimally?** Examples ( The input i.e the no of days can vary ) 5 3 2 => profit = 0 // since the price

Number of all longest increasing subsequences

可紊 提交于 2019-11-27 04:15:52
问题 I'm practicing algorithms and one of my tasks is to count the number of all longest increasing sub-sequences for given 0 < n <= 10^6 numbers. Solution O(n^2) is not an option. I have already implemented finding a LIS and its length (LIS Algorithm), but this algorithm switches numbers to the lowest possible. Therefore, it's impossible to determine if sub-sequences with a previous number (the bigger one) would be able to achieve the longest length, otherwise I could just count those switches, I

Smallest number that can not be formed from sum of numbers from array

╄→尐↘猪︶ㄣ 提交于 2019-11-27 04:14:02
问题 This problem was asked to me in Amazon interview - Given a array of positive integers, you have to find the smallest positive integer that can not be formed from the sum of numbers from array. Example: Array:[4 13 2 3 1] result= 11 { Since 11 was smallest positive number which can not be formed from the given array elements } What i did was : sorted the array calculated the prefix sum Treverse the sum array and check if next element is less than 1 greater than sum i.e. A[j]<=(sum+1). If not

FSharp runs my algorithm slower than Python

牧云@^-^@ 提交于 2019-11-27 04:11:31
问题 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

The minimum number of coins the sum of which is S

大憨熊 提交于 2019-11-27 04:06:06
Given a list of N coins, their values (V1, V2, ... , VN), and the total sum S. Find the minimum number of coins the sum of which is S (we can use as many coins of one type as we want), or report that it's not possible to select coins in such a way that they sum up to S. I try to understand dynamic programming, haven't figured it out. I don't understand the given explanation, so maybe you can throw me a few hints how to program this task? No code, just ideas where I should start. Thanks. This is a classic Knapsack problem, take a look here for some more information: Wikipedia Knapsack Problem