dynamic-programming

Algorithm to generate k element subsets in order of their sum

…衆ロ難τιáo~ 提交于 2019-12-10 10:02:53
问题 If I have an unsorted large set of n integers (say 2^20 of them) and would like to generate subsets with k elements each (where k is small, say 5 ) in increasing order of their sums, what is the most efficient way to do so? Why I need to generate these subsets in this fashion is that I would like to find the k-element subset with the smallest sum satisfying a certain condition, and I thus would apply the condition on each of the k-element subsets generated. Also, what would be the complexity

Understanding the bottom-up rod cut implementation

浪尽此生 提交于 2019-12-10 02:58:26
问题 In Introduction to Algorithms(CLRS), Cormen et al. talk about solving the Rod-cutting problem as follows(page 369) EXTENDED-BOTTOM-UP-CUT-ROD(p, n) let r[0...n] and s[0....n] be new arrays r[0] = 0 for j = 1 to n: q = -infinity for i = 1 to j: if q < p[i] + r[j - i]: // (6) q = p[i] + r[j - i] s[j] = i r[j] = q return r and s Here p[i] is the price of cutting the rod at length i, r[i] is the revenue of cutting the rod at length i and s[i] , gives us the optimal size for the first piece to cut

Broken Calculator

假装没事ソ 提交于 2019-12-10 00:10:49
问题 Problem Statement: There is a broken calculator. Only a few of the digits [ 0 to 9 ] and operators [ +, -, *, / ] are working. A req no. needs to be formed using the working digits and the operators. Each press on the keyboard is called an operation. = operator is always working and is used when the req no. is formed using operators. -1 needs to be printed in case the req no. cannot be formed using the digits and the operators provided OR exceeds the max no. of operations allowed. At no point

Algorithm : allocation of N railway stations in a M villages which are on a straight line

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 23:55:15
问题 I have the next problem I'm having a hard time solving. I have a straight line , and a list of M villages on it. I need to allocate the N railway stations in these villages, so that the average distance of a village from a railway station, will be minimized. example: villages: -----1--------------2--3---------4--5------------6----------7--- and we have 3 railway stations. tried to use dynamic programming, but couldn't do it. can anyone suggest a way to solv this problem? (except the obvious

Efficient algorithm to get the combinations of all items in object

孤者浪人 提交于 2019-12-09 17:21:40
问题 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

Number of ways to make change for amount N

夙愿已清 提交于 2019-12-09 15:30:25
问题 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

Dynamic Programming - Minimum number of coins in C

假装没事ソ 提交于 2019-12-09 14:25:24
问题 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

Given a number N, find the number of ways to write it as a sum of two or more consecutive integers

旧时模样 提交于 2019-12-09 13:09:13
问题 Here is the problem that tagged as dynamic-programming (Given a number N, find the number of ways to write it as a sum of two or more consecutive integers) and example 15 = 7+8, 1+2+3+4+5, 4+5+6 I solved with math like that : a + (a + 1) + (a + 2) + (a + 3) + ... + (a + k) = N (k + 1)*a + (1 + 2 + 3 + ... + k) = N (k + 1) a + k (k+1)/2 = N (k + 1)*(2*a + k)/2 = N Then check that if N divisible by (k+1) and (2*a+k) then I can find answer in O(sqrt(N)) time Here is my question how can you solve

Minimum Steps to One

家住魔仙堡 提交于 2019-12-09 09:09:03
问题 Problem statement : On a positive integer, you can perform any one of the following 3 steps. Subtract 1 from it. ( n = n - 1 ) If its divisible by 2, divide by 2. ( if n % 2 == 0 , then n = n / 2 ) If its divisible by 3, divide by 3. ( if n % 3 == 0 , then n = n / 3 ). Now the question is, given a positive integer n, find the minimum number of steps that takes n to 1 eg: For n = 1 , output: 0 For n = 4 , output: 2 ( 4 /2 = 2 /2 = 1 ) For n = 7 , output: 3 ( 7 -1 = 6 /3 = 2 /2 = 1 ) I know the

given an array of integers in random order you have to find the minimum number of swaps to convert it to cyclic sorted array

ε祈祈猫儿з 提交于 2019-12-09 04:51:55
问题 if an array is given in random order , you have to output the minimum number of swaps required to convert into cyclic sorted array. e.g. array given is 3 5 4 2 1 so the first swap will be 5<-->4 result : 3 4 5 2 1 second swap will be 2<-->1 result : 3 4 5 1 2 (final) output : 2 i am not able to get the logic behind this problem. adding some more : swap only possible between adjacent elements and numbers are between range 1 to N 回答1: Well, don't know if it is the best algorithm available, but