dynamic-programming

Algorithm for Filling bag maximally (this is not the knapsack 0/1)

拟墨画扇 提交于 2019-12-06 13:57:26
I am working on some task which requires from me to solve the following algorithmic problem: - You Have collection of items (their weights): [w1, w2, ..., wn] - And You have a bag which weight is: W - It is Needed to fill the bag maximally (fill as much as possible) with the subset of given items. So this is not "Knapsack 0/1" problem, as we deal only with weights (we have only one parameter for item). Therefore I assume that it might have a solution (Knapsack is NP-complete) or some kind of algorithm which gives approximately correct result. Please don't suggest me "greedy algorithms",

Minimum tip to be paid for bill amount B with two kind of coins (x,y) only

风格不统一 提交于 2019-12-06 13:36:03
问题 I have two kind of coins (unlimited coins of each type). The values of these two coins are x and y . I have to pay a bill of amount B . What minimum amount i will need to pay as tip . tip can be any value >=0 The objective is to minimize the tip. I just was thinking about the Dynamic programming approach.Or any Faster method. Please help. function minTip(x,y,B){ if(z<=0) return -z; return minimum( minTip(x,y,B-x),minTip(x,y,B-y) ); } Can any one help with the DP approach.?? 回答1: You don't

How to find the count of numbers which are divisible by 7?

自闭症网瘾萝莉.ら 提交于 2019-12-06 11:48:24
问题 Given an integer N , how to efficiently find the count of numbers which are divisible by 7 (their reverse should also be divisible by 7) in the range: [0, 10^N - 1] Example: For N=2 , answer: 4 {0, 7, 70, 77} [All numbers from 0 to 99 which are divisible by 7 (also their reverse is divisible)] My approach, simple brute-force: initialize count to zero run a loop from i=0 till end if a(i) % 7 == 0 && reverse(a(i)) % 7 == 0 , then we increase the count Note: reverse(123) = 321 , reverse(1200) =

Can I avoid Python loop overhead on dynamic programming with numpy?

邮差的信 提交于 2019-12-06 11:29:21
I need help with the Pythonic looping overhead of the following problem: I'm writing a function that calculates a pixel flow algorithm that's a classic dynamic programming algorithm on a 2D Numpy array. It requires: 1) visiting all the elements of the array at least once like this: for x in range(xsize): for y in range(ysize): updateDistance(x,y) 2) potentially following a path of elements based on the values of the neighbors of an element which looks like this while len(workingList) > 0: x,y = workingList.pop() #if any neighbors of x,y need calculation, push x,y and neighbors on workingList

Dynamic Programming and the 0/1 knapsack

风格不统一 提交于 2019-12-06 10:51:47
问题 I'm having some trouble understanding dynamic programming, even though I've read through so many resources trying to understand. I understand an example given of dynamic programming using the fibonacci algorithm. I understand how if you use the divide and conquer approach to it, you'll end up solving some sub-problems multiple times, and dynamic programming takes care of that by solving those overlapping subproblems but only once (and storing them for future reference). However, I have been

Ruby dynamic arguments in dynamically created methods

随声附和 提交于 2019-12-06 10:41:37
问题 I have the following kind of method definition: method_name = :foo method_arguments = [:bar, :baz] method_mandatory_arguments = {:quux => true} method_body = ->{ quux ? bar + baz : bar - baz } So I want to get a real method. But define_method has no any possibility to define method arguments dynamically. I know another way to use class_eval but I know than defining methods with class_eval is much slower than define_method. How I can effectively archive this? I did some benchmarks in rails

Increase set of numbers so that XOR sum is 0

我的梦境 提交于 2019-12-06 10:26:22
问题 I need some help with a problem that I have reduced to the following. I have N 30 bit numbers, such that the combined XOR of all of them is non-zero. I need to add a non-negative (0 or more) value to each of the N numbers, such that the combined XOR of the new numbers becomes 0, under the constraint that the total addition value (not the number of additions) is minimized. For example, if I had numbers (01010) 2 , (01011) 2 and (01100) 2 as three numbers (N = 3). Then, their combined XOR is

Partitioning a list of integers to minimize difference of their sums

老子叫甜甜 提交于 2019-12-06 10:21:35
问题 Given a list of integers l , how can I partition it into 2 lists a and b such that d(a,b) = abs(sum(a) - sum(b)) is minimum. I know the problem is NP-complete, so I am looking for a pseudo-polynomial time algorithm i.e. O(c*n) where c = sum(l map abs) . I looked at Wikipedia but the algorithm there is to partition it into exact halves which is a special case of what I am looking for... EDIT: To clarify, I am looking for the exact partitions a and b and not just the resulting minimum

How many possible scorecards are consistent with the input scorecard?

空扰寡人 提交于 2019-12-06 07:34:09
问题 I have been trying to solve the following problem in interview street. Count Scorecards(30 points) In a tournament, N players play against each other exactly once. Each game results in either of the player winning. There are no ties. You have given a scorecard containing the scores of each player at the end of the tournament. The score of a player is the total number of games the player won in the tournament. However, the scores of some players might have been erased from the scorecard. How

Algorithm for matching point sets

房东的猫 提交于 2019-12-06 07:06:10
问题 I have two sets of points A and B , whereas the points can be 2D or 3D. Both sets have the same size n , which is rather low (5 - 20). I would like to know how well these sets agree. That is, ideally I would find pairings between the points such that the sum of all Euclidean pair distances d(A,B) is minimal. So d(A,B) = \sum_{i=1}^n ||A_i - B_i||_2 The final outcome is used to compare with other point sets. So, for example: A = (1,1), (1,2), (1,3) B = (1,1), (2,2), (1,3) would give me d(A,B)