寻找最低硬币总量的最优变化

时间:2013-10-27 23:48:38

标签: python optimization brute-force

所以这是涉及硬币的两部分问题。第一部分涉及总计1-99美分的硬币数量(例如,它需要1个硬币才能达到1美分,2个硬币要达到2美分等等,并添加所需的硬币总额。每个值)。这可以通过以下代码表示(随意提出建议/改进):

def findbest(origarray, denom):
    current = origarray
    i = 1
    while(i < size):
        if(i in denom):
            current[i] = 1
            coinlist[i] = [i]
        else:
            k = 1
            while(k < 1 + (i/2)):
                c = current[k] + current[i-k]
                if(c < current[i]):
                    current[i] = c
                    coinlist[i] = coinlist[k] + coinlist[i-k]
                k+=1
        print i, current[i], coinlist[i]
        i+=1
    return current


size = 100
coinlist = [[]]
origarray = [0] 
i = 1

while(i < size):
    origarray.append(100)
    coinlist.append([])
    i += 1

denom = [1,5,10,25,50]

x = findbest(origarray, denom)

total=0

for value in findbest(origarray,denom):
    total += value

print total


print "\n\n\n"
print x

问题的第二部分是找到理想的三种面额(不必是真正的面额,但必须是1),这将产生所有硬币计数的最低总和。 这对我来说很棘手。我知道我必须写一些会强制命名值的东西,直到它找到最佳值(我知道[1,12,19],我只是无法达到那一点),但我不是确定如何去做。有没有人知道如何做到这一点?

2 个答案:

答案 0 :(得分:1)

你正在寻找的功能,这将使这完全无关紧要itertools.combinations

>>> from itertools import combinations
>>> len(list(a for a in combinations(range(1, 101), 3)))
161700

我建议你的实现基于你的实现:

def findbest(origarray, denom):
    current = origarray
    i = 1
    while(i < size):
        if(i in denom):
            current[i] = 1
            coinlist[i] = [i]
        else:
            k = 1
            while(k < 1 + (i/2)):
                c = current[k] + current[i-k]
                if(c < current[i]):
                    current[i] = c
                    coinlist[i] = coinlist[k] + coinlist[i-k]
                k+=1
        #print i, current[i], coinlist[i]
        i+=1
    return current

size = 100

def reset_cache():
  i = 1
  global coinlist
  coinlist = [[]]
  global origarray
  origarray = [0] 

  while(i < size):
      origarray.append(100)
      coinlist.append([])
      i += 1

reset_cache()

denom = [1,5,10,25,50]

x = findbest(origarray, denom)

total=0

for value in findbest(origarray,denom):
    total += value

print total


print "\n\n\n"
print x


from itertools import combinations

best = ((0,0,0), 1e6)
for comb in combinations(range(1, 101), 3):
  #print "Considering: %s" % comb
  reset_cache()
  total = 0
  for value in findbest(origarray, comb):
    total += value
  if total < best[1]:
    print "%s beat best with %d" % (comb, total)
    best = (comb, total)

print best

但是我不知道需要什么我认为是硬币缓存?我不确定,我没有太难读你的代码。但我不喜欢传递几个数组来使其工作的必要性。它应该是独立的。

编辑:在我看来,你实际上可以逃脱

for comb in [(1,) + a for a in combinations(range(2, 101), 2)]:

因为任何有效的变更系统都需要1美分硬币。这使代码运行得更快,因为

>>> len([(1,) + a for a in combinations(range(2, 101), 2)])
4851

答案 1 :(得分:1)

我的java版

public static int minChange(int[] coins, int total) {
        int[] counts = new int[total + 1];
        counts[0] = 0;
        int MAX = Integer.MAX_VALUE - 1;
        for (int i = 1; i <= total; ++i) {
            int count = MAX;
            for (int j = 0; j < coins.length; ++j) {
                if (i - coins[j] >= 0 && count > counts[i - coins[j]])
                    count = counts[i - coins[j]];
            }
            if (count < MAX)
                counts[i] = count + 1;
            else
                counts[i] = MAX;
        }
        return counts[total];
    }