dynamic-programming

Efficient algorithm to get the combinations of all items in object

筅森魡賤 提交于 2019-12-04 05:11:24
Given an array or object with n keys, I need to find all combinations with length x . Given X is variable. binomial_coefficient(n,x) . Currently I'm using this: function combine(items) { var result = []; var f = function(prefix, items) { for (var i = 0; i < items.length; i++) { result.push(prefix + items[i]); f(prefix + items[i], items.slice(i + 1)); } } f('', items); return result; } var combinations = combine(["a", "b", "c", "d"]); The output is: ["a", "ab", "abc", "abcd", "abd", "ac", "acd", "ad", "b", "bc", "bcd", "bd", "c", "cd", "d"] So if I want the binomial coefficient x=3 from n=4 I

recursively implementing 'minimum number of coins' in python

◇◆丶佛笑我妖孽 提交于 2019-12-04 03:41:27
问题 This problem is same as asked in here. Given a list of coins, their values (c1, c2, c3, ... cj, ...), and the total sum i. Find the minimum number of coins the sum of which is i (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"m just introduced to dynamic programming yesterday and I tried to make a code for it. # Optimal substructure: C[i] = 1 + min_j(C[i-cj]) cdict = {} def C(i, coins): if i <= 0:

Vectorizing sums of different diagonals in a matrix

谁说我不能喝 提交于 2019-12-04 03:03:45
问题 I want to vectorize the following MATLAB code. I think it must be simple but I'm finding it confusing nevertheless. r = some constant less than m or n [m,n] = size(C); S = zeros(m-r,n-r); for i=1:m-r+1 for j=1:n-r+1 S(i,j) = sum(diag(C(i:i+r-1,j:j+r-1))); end end The code calculates a table of scores, S , for a dynamic programming algorithm, from another score table, C . The diagonal summing is to generate scores for individual pieces of the data used to generate C , for all possible pieces

Number of ways to make change for amount N

戏子无情 提交于 2019-12-04 02:46:26
I came across this problem: http://www.geeksforgeeks.org/dynamic-programming-set-7-coin-change/ Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins, how many ways can we make the change? The order of coins doesn’t matter. For example, for N = 4 and S = {1,2,3}, there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}. So output should be 4. For N = 10 and S = {2, 5, 3, 6}, there are five solutions: {2,2,2,2,2}, {2,2,3,3}, {2,2,6}, {2,3,5} and {5,5}. So the output should be 5. I came up with the solution: //

Dynamic Programming - Minimum number of coins in C

限于喜欢 提交于 2019-12-03 23:37:16
I have looked through various questions on the site and I haven't managed to find anything which implements this by the following reasoning (so I hope this isn't a duplicate). The problem I'm trying to solve via a C program is the following: As the programmer of a vending machine controller your are required to compute the minimum number of coins that make up the required change to give back to customers. An efficient solution to this problem takes a dynamic programming approach, starting off computing the number of coins required for a 1 cent change, then for 2 cents, then for 3 cents, until

Dynamic Programming - Word Break

拈花ヽ惹草 提交于 2019-12-03 23:09:41
问题 I am trying to solve this Problem.The question is as follows Given an input string and a dictionary of words, find out if the input string can be segmented into a space-separated sequence of dictionary words. Dictionary is an array of strings. My Approach is the following recursive fn with storing of the results of recursive calls. The output is fine but I see that the stored result is never used. My solution is hopefully correct as it passed the test cases.But I would be great if I know

Knapsack with minimum cost

跟風遠走 提交于 2019-12-03 21:59:21
I've got several types of coins, each have weight (wi) and cost (ci). So I've got to make a knapsack with weight==W and (!) minimum cost of coins in it. I can`t make recurrence relation to use DP. Just formulate the usual recurrence relation... Designate the minimum cost achievable with total weight k as Min_cost(k). Bootstrap the memoization with: Min_cost(0) = cost of empty set = 0 Then solve for increasing values of k using: Min_cost(i+1) = min [Min_cost(i) + min [ci, for all items with wi = 1], Min_cost(i-1) + min [ci, for all items with wi = 2], Min_cost(i-2) + min [ci, for all items with

Recursion on staircase

给你一囗甜甜゛ 提交于 2019-12-03 20:55:01
I'm trying to understand the solution provided in a book to the following question: "A child is running up a staircase with n steps and can hop either 1 step, 2 steps or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs." The book's solution is as follows, stemming from the fact that "the last move may be a single step hop from n - 1, a double step hop from step n - 2 or a triple step hop from step n - 3" public static int countWaysDP(int n, int[] map) { if (n < 0) return 0; else if (n == 0) return 1; else if (map[n] > -1) return map[n]; else

Dynamic Programming Solution for a Variant of Coin Exchange

喜夏-厌秋 提交于 2019-12-03 19:42:33
问题 I am practicing Dynamic Programming. I am focusing on the following variant of the coin exchange problem: Let S = [1, 2, 6, 12, 24, 48, 60] be a constant set of integer coin denominations. Let n be a positive integer amount of money attainable via coins in S . Consider two persons A and B . In how many different ways can I split n among persons A and B so that each person gets the same amount of coins (disregarding the actual amount of money each gets)? Example n = 6 can be split into 4

Bytelandian Gold Coin , Dynamic programming , explanation?

故事扮演 提交于 2019-12-03 19:40:23
问题 It's a bit immature, but I have to ask, The Bytelandian Gold coin problem mentioned here - http://www.codechef.com/problems/COINS/ , is said to be typical DP problem,even though I have read basics of DP & recursion, but I am finding hard to understand its solution, # include <stdio.h> # include <stdlib.h> long unsigned int costArray[30][19]; unsigned int amount; unsigned int currentValue(short int factor2,short int factor3) { int j; unsigned int current = amount >> factor2; for(j=0;j<factor3