0

I have a python function from

https://stackoverflow.com/a/44214566/6301603

Which calculates the amount of possible coin changes. Where each coin has a limited avaibility. I tried to understand it to convert it to java but i failed in the line r= can somebody explain whats happening in this line?

# cs is a list of pairs (c, k) where there's k
# coins of value c.
def limited_coins(cs, n):
    r = [1] + [0] * n
    for c, k in cs:
        # rs[i] will contain the sum r[i] + r[i-c] + r[i-2c] + ...
        rs = r[:]
        for i in xrange(c, n+1):
            rs[i] += rs[i-c]
            # This line effectively performs:
            # r'[i] = sum(r[i-j*c] for j=0...k)
            # but using rs[] so that the computation is O(1)
            # and in place.
            r[i] += rs[i-c] - (0 if i<c*(k+1) else rs[i-c*(k+1)])
    return r[n]

for n in xrange(50):
    print n, limited_coins([(1, 3), (2, 2), (5, 3), (10, 2)], n)

Thanks

AcK
  • 2,063
  • 2
  • 20
  • 27
TTho Einthausend
  • 609
  • 4
  • 13
  • 1
    Failed in what way? It concatenates a list containing 1 to n lists containing zeros. There is no such feature in java - you have to write a routine to do it. – cup Oct 11 '20 at 14:46
  • 2
    `r = [1] + [0] * n` builds a list like so `[1, 0, 0, 0]` when `n == 3` – AcK Oct 11 '20 at 14:47

1 Answers1

0

Replace that line with

int[] r = new int[n+1];
r[0] = 1;
for (int i = 1; i < r.length; i++) 
    r[i] = 0;
Bhushan Oza
  • 112
  • 1
  • 10