dynamic-programming

How to calculate the best price [duplicate]

左心房为你撑大大i 提交于 2019-12-04 18:32:01
This question already has answers here : Selecting A combination of minimum cost (5 answers) Closed 4 years ago . I have a interesting problem. I would like to know some good approaches to solve this. I have a small store in which I have 'n' number of products Each product as a non zero price associated with it A product looks like this { productId:productA; productName:ABC; price:20$ } To enhance the customer retention, I would like to bring in combo model to sell. That is, I define m number of combos Each combo looks like this { comboId:1; comboName:2; constituentProducts: {Product1,

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

故事扮演 提交于 2019-12-04 18:14:27
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 way of trying all the options)? I assume that, for each village, you care only about the distance from

Unable to implement a dynamic programming table algorithm in python

て烟熏妆下的殇ゞ 提交于 2019-12-04 18:12:52
I am having problems creating a table in python. Basically I want to build a table that for every number tells me if I can use it to break down another(its the table algo from the accepted answer in Can brute force algorithms scale? ). Here's the pseudo code: for i = 1 to k for z = 0 to sum: for c = 1 to z / x_i: if T[z - c * x_i][i - 1] is true: set T[z][i] to true Here's the python implementation I have: from collections import defaultdict data = [1, 2, 4] target_sum = 10 # T[x, i] is True if 'x' can be solved # by a linear combination of data[:i+1] T = defaultdict(bool) # all values are

Trying to figure out the classic Knapsack recurrence

纵饮孤独 提交于 2019-12-04 17:34:58
I am reading about the Knapsack Problem (unbounded) which is, as I understand a classic in DP. Although I think I understand the solution as I read it, I am not clear how I can translate it to actual code. For example in the following recurrence "formula": M(j) = MAX {M(j-1), MAX i = 1 to n (M(j - Si) + Vi) } for j >=1 I am not sure how I can translate this to code, since it is not clear to me if the inner MAX should be there or it should just be instead: M(j) = MAX {M(j-1), M(j - Si) + Vi } for j >=1 Any help to figure out the formula and to code it? you can code it like this: for w = 0 to W

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

风格不统一 提交于 2019-12-04 17:27:04
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) = 21 , for example! There is a recursive solution using digit dp technique for any digits. long long call

Minimum repetitions in merged array of characters

时间秒杀一切 提交于 2019-12-04 16:56:09
Suppose I have two arrays and I want to merge them so that the merged array has the minimum amount of repetitions . For example [ 'x', 'x' ] is a repetition. arr1 = [ 'x', 'd', 'd', 'm', 'f', 'm' ] arr2 = [ 'd', 'd', 'x', 'f', 'f', 'm' ] The only condition is that in the merged array, the elements from arr1 and arr2 must appear in their respective orders within arr1 and arr2 . Below is an example of the merged array with 0 repetitions while maintaining this condition. merged = [ 'd', 'x', 'd', 'x', 'd', 'f', 'd', 'm', 'f', 'm', 'f', 'm' ] I'm trying to relate this problem to popular dynamic

Ruby dynamic arguments in dynamically created methods

僤鯓⒐⒋嵵緔 提交于 2019-12-04 15:34:24
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 console: class Foo; end n = 100_000 Benchmark.bm do |x| x.report('define_method') do n.times { |i| Foo

Partitioning a list of integers to minimize difference of their sums

ぃ、小莉子 提交于 2019-12-04 14:50:57
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 difference d(a, b) To generalize, what is a pseudo-polynomial time algorithm to partition a list of n numbers

Subsequence sum and GCD

丶灬走出姿态 提交于 2019-12-04 14:34:56
问题 I came across this question on a programming challenge about a month ago, but the editorial wasn't released so I am asking it here. There is an Array A of size N. Find the sum * GCD of K length subsequences of A. Example: If A = [1, 2, 3] and K = 2, {1, 2} = 3(sum) * 1(GCD) = 3 {1, 3} = 4(sum) * 1(GCD) = 4 {2, 3} = 5(sum) * 1(GCD) = 5 Ans => 3 + 4 + 5 = 12 回答1: Here it is from scratch (didn't test it thoroughly though): Let C[k, i, d] be the number of all k -length subsequences of A[1..i]

5 CPU's Task scheduling N process

瘦欲@ 提交于 2019-12-04 14:27:53
问题 Question. There are 5 CPUs and N number of tasks in the queue. You have to use minimum CPUs to process the tasks. A task is of format [arrival time, time to process the task]. Note: You can only use at most 5 CPUs. If it is not possible in 5 CPUs, print -1. The time to process the task should not be greater than 10 i.e (Time waiting in queue + time to process the task) <= 10. If to process the current task, you need more than 10 seconds in current CPU, you can move to a different CPU and