dynamic-programming

n steps with 1, 2 or 3 steps taken. How many ways to get to the top?

时光毁灭记忆、已成空白 提交于 2019-11-28 05:30:15
If we have n steps and we can go up 1 or 2 steps at a time, there is a Fibonacci relation between the number of steps and the ways to climb them. IF and ONLY if we do not count 2+1 and 1+2 as different. However, this no longer the case, as well as having to add we add a third option, taking 3 steps. How do I do this? What I have: 1 step = 1 way 2 steps = 2 ways: 1+1, 2 3 steps = 4 ways: 1+1+1, 2+1, 1+2, 3 I have no idea where to go from here to find out the number of ways for n stairs I get 7 for n = 4 and 14 for n= 5 i get 14+7+4+2+1 by doing the sum of all the combinations before it. so ways

Dynamic Programming - Number of distinct combinations to reach a given score

心已入冬 提交于 2019-11-28 05:03:23
问题 Consider a game where a player can score 3 or 5 or 10 points in a move. Given a total score n, find number of 'distinct' combinations to reach the given score. My code: #include <iostream> #include<unordered_map> using namespace std; unordered_map<int,int> m; int numOfWays(int n){ if(n==0) return 1; if(n<0) return 0; if(m[n]>0) return m[n]; m[n] = numOfWays(n-3)+numOfWays(n-5)+numOfWays(n-10); return m[n]; } int main(){ int t; cin>>t; cout<<numOfWays(t)<<endl; return 0; } For input 11, I am

How can a recursive Java method be memoized?

大憨熊 提交于 2019-11-28 04:43:13
问题 So I've built this program to build different stair cases. Essentially the problem is: Given an integer N, how many different ways can you build the staircase. N is guaranteed to be larger than 3 and smaller than 200. Any previous step can not be larger than its following step otherwise it defeats the purpose of the staircase. So given N = 3 You can build one staircase: 2 steps and then 1 step following that Given N = 4 You can build one staircase: 3 steps and then 1 step following that Given

Maximum Contiguous Subsequence Sum of At Least Length L

僤鯓⒐⒋嵵緔 提交于 2019-11-28 04:01:23
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? 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 maximum-sum subarray of the original sequence. This can be solved by Kadane's algorithm, a clever dynamic

How are Dynamic Programming algorithms implemented in idiomatic Haskell?

余生长醉 提交于 2019-11-28 03:23:26
Haskell and other functional programming languages are built around the premise of not maintaining state. I'm still new to how functional programming works and concepts in it, so I was wondering if it is possible to implement DP algorithms in an FP way. What functional programming constructs can be used to do that? The common way to do this is via lazy memoization. In some sense, the recursive fibonacci function can be considered dynamic programming, because it computes results out of overlapping subproblems. I realize this is a tired example, but here's a taste. It uses the data

Sum of digits of a factorial

为君一笑 提交于 2019-11-28 02:57:21
Link to the original problem It's not a homework question. I just thought that someone might know a real solution to this problem. I was on a programming contest back in 2004, and there was this problem: Given n, find sum of digits of n!. n can be from 0 to 10000. Time limit: 1 second. I think there was up to 100 numbers for each test set. My solution was pretty fast but not fast enough, so I just let it run for some time. It built an array of pre-calculated values which I could use in my code. It was a hack, but it worked. But there was a guy, who solved this problem with about 10 lines of

Why is the knapsack problem pseudo-polynomial?

走远了吗. 提交于 2019-11-28 02:52:35
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 ? marcog 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 it pseudo -polynomial. Consider W = 1,000,000,000,000. It only takes 40 bits to represent this

Difference between Divide and Conquer Algo and Dynamic Programming

╄→гoц情女王★ 提交于 2019-11-28 02:38:54
What is the difference between Divide and Conquer Algorithms and Dynamic Programming Algorithms? How are the two terms different? I do not understand the difference between them. Please take a simple example to explain any difference between the two and on what ground they seem to be similar. OneMoreError Divide and Conquer Divide and Conquer works by dividing the problem into sub-problems, conquer each sub-problem recursively and combine these solutions. Dynamic Programming Dynamic Programming is a technique for solving problems with overlapping subproblems. Each sub-problem is solved only

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

无人久伴 提交于 2019-11-28 02:30:55
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: Getting the lowest possible sum from numbers' difference So, can you suggest me good books or articles (preferably with examples with real code) which would explain me what is dynamic programming? I really want simple examples first of all, then I'll move on. Ian Bishop Dynamic programming is a useful type of algorithm that can be used to optimize hard problems by breaking them up into smaller subproblems. By storing and re-using partial solutions, it manages

How do I replace a method implementation at runtime?

a 夏天 提交于 2019-11-28 02:08:15
I'd like to have property getters and methods that I can decorate with my own custom attribute and based on the presence of that attribute replace the method bodies with a different implementation. Also, that different implementation will need to know the constructor arguments given to the custom attribute where it decorates the method. This can obviously be done with AOP, like PostSharp or LinFu, but I'm wondering if there's a way to do this that does not involve a post-build processing step because adding that complicates the project more than I would prefer. Using the traditional .Net APIs