dynamic-programming

Maximizing profit for given stock quotes

懵懂的女人 提交于 2019-11-28 15:23: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 decreases each day ,the max profit we can make = 0 1 2 100 => profit = 197 1 3 1 2 =>profit = 3 // we buy at 1

A simple example for someone who wants to understand Dynamic Programming [closed]

落花浮王杯 提交于 2019-11-28 14:59:40
I am looking for a manageably understandable example for someone who wants to learn Dynamic Programming. There are nice answers here about what is dynamic programming . The fibonacci sequence is a great example, but it is too small to scratch the surface. It looks a great subject to learn about although I haven't taken the algorithms class yet, hopefully it is on my list for the spring. Check out this site: Dynamic Programming Practice Problems Here is a good tutorial comprising 29 solved DP problem with great explanation. The idea behind dynamic programming is that you're caching (memoizing)

Dynamic Programming - making change

≯℡__Kan透↙ 提交于 2019-11-28 12:22:35
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 stored in the table to solve the larger problem without using recursion. public static int minCoins

Maximise sum of “non-overlapping” numbers from matrix

 ̄綄美尐妖づ 提交于 2019-11-28 10:20:32
Just looking for a bit of direction, I realise that the example given is possible to solve using brute force iteration, but I am looking for a more elegant (ie. mathematical?) solution which could potentially solve significantly larger examples (say 20x20 or 30x30). It is entirely possible that this cannot be done, and I have had very little success in coming up with an approach which does not rely on brute force... I have a matrix (call it A) which is nxn. I wish to select a subset (call it B) of points from matrix A. The subset will consist of n elements, where one and only one element is

How to find the shortest simple path in a Tree in a linear time?

人走茶凉 提交于 2019-11-28 09:22:29
Here is a problem from Algorithms book by Vazirani The input to this problem is a tree T with integer weights on the edges. The weights may be negative, zero, or positive. Give a linear time algorithm to find the shortest simple path in T. The length of a path is the sum of the weights of the edges in the path. A path is simple if no vertex is repeated. Note that the endpoints of the path are unconstrained. HINT: This is very similar to the problem of finding the largest independent set in a tree. How can I solve this problem in linear time? Here is my algorithm but I'm wonder if it is linear

Number of ways to add up to a sum S with N numbers

偶尔善良 提交于 2019-11-28 07:34:09
Say S = 5 and N = 3 the solutions would look like - <0,0,5> <0,1,4> <0,2,3> <0,3,2> <5,0,0> <2,3,0> <3,2,0> <1,2,2> etc etc. In the general case, N nested loops can be used to solve the problem. Run N nested loop, inside them check if the loop variables add upto S. If we do not know N ahead of time, we can use a recursive solution. In each level, run a loop starting from 0 to N, and then call the function itself again. When we reach a depth of N, see if the numbers obtained add up to S. Any other dynamic programming solution? Try this recursive function: f(s, n) = 1 if s = 0 = 0 if s != 0 and

How can I find the maximum sum of a sub-sequence using dynamic programming?

佐手、 提交于 2019-11-28 07:19:32
问题 I'm re-reading Skiena's Algorithm Design Manual to catch up on some stuff I've forgotten since school, and I'm a little baffled by his descriptions of Dynamic Programming. I've looked it up on Wikipedia and various other sites, and while the descriptions all make sense, I'm having trouble figuring out specific problems myself. Currently, I'm working on problem 3-5 from the Skiena book. (Given an array of n real numbers, find the maximum sum in any contiguous subvector of the input.) I have an

algorithm to find longest non-overlapping sequences

亡梦爱人 提交于 2019-11-28 06:55:40
I am trying to find the best way to solve the following problem. By best way I mean less complex. As an input a list of tuples (start,length) such: [(0,5),(0,1),(1,9),(5,5),(5,7),(10,1)] Each element represets a sequence by its start and length , for example (5,7) is equivalent to the sequence (5,6,7,8,9,10,11) - a list of 7 elements starting with 5. One can assume that the tuples are sorted by the start element. The output should return a non-overlapping combination of tuples that represent the longest continuous sequences(s). This means that, a solution is a subset of ranges with no overlaps

3-PARTITION problem

为君一笑 提交于 2019-11-28 06:36:56
here is another dynamic programming question ( Vazirani ch6 ) Consider the following 3-PARTITION problem. Given integers a1...an, we want to determine whether it is possible to partition of {1...n} into three disjoint subsets I, J, K such that sum(I) = sum(J) = sum(K) = 1/3*sum(ALL) For example, for input (1; 2; 3; 4; 4; 5; 8) the answer is yes, because there is the partition (1; 8), (4; 5), (2; 3; 4). On the other hand, for input (2; 2; 3; 5) the answer is no. Devise and analyze a dynamic programming algorithm for 3-PARTITION that runs in time poly- nomial in n and (Sum a_i) How can I solve

Given an array of length n, find number of subsets where XOR of a subset is equal to a given number [closed]

二次信任 提交于 2019-11-28 05:39:16
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . Given an array, arr , of length n , find how many subsets of arr there are such that XOR(^) of those subsets is equal to a given number, ans . I have this dp approach but is there a way to improve its time complexity. ans is always less than 1024. Here ans is the no. such that XOR(^) of the subsets