dynamic-programming

Dynamic programming sum

不羁的心 提交于 2019-11-28 01:03:09
问题 How would you use dynamic programming to find the list of positive integers in an array whose sum is closest to but not equal to some positive integer K? I'm a little stuck thinking about this. 回答1: The usual phrasing for this is that you're looking for the value closest to, but not exceeding K. If you mean "less than K", it just means that your value of K is one greater than the usual. If you truly mean just "not equal to K", then you'd basically run through the algorithm twice, once finding

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

别等时光非礼了梦想. 提交于 2019-11-27 23:22:49
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 <algorithm> typedef std::pair<std::string, std::string> StrPair; class Person :private StrPair { public:

Divide array into k contiguos partitions such that sum of maximum partition is minimum

左心房为你撑大大i 提交于 2019-11-27 22:44:50
问题 Here maximum sum subset is one of k subsets that give maximum sum e.g: arr = [10,5,3,7] and k = 2 possible ways to divide arr in k subsets is {10,[5,3,7]},{[10,5],[3,7},{[10,5,3],7} and {[10,5],[3,7} is the optimal one. Edit: it is equivalent of https://www.codechef.com/DI15R080/problems/MINMAXTF 回答1: Assume you know the answer is x which means sum of the maximum subset is equal to x . You can verify this assumption by a greedy algorithm O(n) . (Traverse the array from left to right and pick

Understanding change-making algorithm

不羁的心 提交于 2019-11-27 21:00:52
问题 I was looking for a good solution to the Change-making problem and I found this code(Python): target = 200 coins = [1,2,5,10,20,50,100,200] ways = [1]+[0]*target for coin in coins: for i in range(coin,target+1): ways[i]+=ways[i-coin] print(ways[target]) I have no problems understanding what the code literally does,but I can't understand WHY it works. Anyone can help? 回答1: To get all possible sets that elements are equal to 'a' or 'b' or 'c' (our coins) that sum up to 'X' you can: Take all

How can I find the minimum index of the array in this case?

大城市里の小女人 提交于 2019-11-27 20:43:28
问题 We are given an array with n values. Example: [1,4,5,6,6] For each index i of the array a ,we construct a new element of array b such that, b[i]= [a[i]/1] + [a[i+1]/2] + [a[i+2]/3] + ⋯ + [a[n]/(n−i+1)] where [.] denotes the greatest integer function. We are given an integer k as well. We have to find the minimum i such that b[i] ≤ k . I know the brute-force O(n^2) algorithm (to create the array - 'b'), can anybody suggest a better time complexity and way solve it? For example, for the input

String Reduction - Programming Contest . Solution needed

流过昼夜 提交于 2019-11-27 20:35:40
问题 I have a question which asks us to reduce the string as follows. The input is a string having only A , B or C . Output must be length of the reduced string The string can be reduced by the following rules If any 2 different letters are adjacent, these two letters can be replaced by the third letter. Eg ABA -> CA -> B . So final answer is 1 (length of reduced string) Eg ABCCCCCCC This doesn't become CCCCCCCC , as it can be reduced alternatively by ABCCCCCCC -> AACCCCCC -> ABCCCCC -> AACCCC ->

Finding all paths down stairs?

和自甴很熟 提交于 2019-11-27 20:09:37
问题 I was given the following problem in an interview: Given a staircase with N steps, you can go up with 1 or 2 steps each time. Output all possible way you go from bottom to top. For example: N = 3 Output : 1 1 1 1 2 2 1 When interviewing, I just said to use dynamic programming. S(n) = S(n-1) +1 or S(n) = S(n-1) +2 However, during the interview, I didn't write very good code for this. How would you code up a solution to this problem? Thanks indeed! 回答1: You can generalize your recursive

How are Dynamic Programming algorithms implemented in idiomatic Haskell?

孤者浪人 提交于 2019-11-27 19:14:47
问题 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? 回答1: 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

Number of all longest increasing subsequences

时光怂恿深爱的人放手 提交于 2019-11-27 18:12:53
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 guess. Any ideas how to get this in about O(nlogn) ? I know that it should be solved using dynamic

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

故事扮演 提交于 2019-11-27 17:53:16
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 so then answer would be sum+1 But this was nlog(n) solution. Interviewer was not satisfied with this and