简体   繁体   中英

How many different ways to fill N number bins with B number of balls

Assume you have N number of bins, where the capacity of each in bin is K. You also have B number of balls. How many different ways can all the balls be distributed into the bins?

I'm trying to solve this problem by writing a function that takes in the following parameters:

public static int waysBin(int ball, int bin, int capacity) 
   {
      //code here

   } 

I'm a bit unsure as to how to approach this. I know when N = 0, the answer is 0. And when B = 0, N> 1, the answer is 1.

However, I'm not sure of how to calculate it for every other combination. I'd like to solve this both recursively and dynamically.

Think of it this way: if you have n balls filling b bins of capacity k then you can fill the first bin with between 0 and k balls (call that number c). For each of these possibilities you can fill the remaining b-1 bins with nc balls. If you have only 1 bin then there is one combination if you have fewer balls than the capacity, zero otherwise.

So:

int combinations(int ballCount, int binCount, int binSize) {
    if (binCount > 1) {
        return IntStream.rangeClosed(0, Math.min(ballCount, binSize))
            .map(c -> combinations(ballCount - c, binCount - 1, binSize))
            .sum();
    } else {
        return binCount == 0 || ballCount > binSize ? 0 : 1;
    }            
}

If you do not have Java 8 use:

int combinations(int ballCount, int binCount, int binSize) {
    if (binCount > 1) {
        int combos = 0;
        for (c = 0; c <= Math.min(ballCount, binSize); c++)
            combos += combinations(ballCount - c, binCount - 1, binSize);
        return combos;
    } else {
        return binCount == 0 || ballCount > binSize ? 0 : 1;
    }            
}

An idea:

Make a class to represent a Bin. It will have an int representing how many balls are inside it.

Make a class called BinState. It will have a List of bins with length N.

Then a recursive function that will look like this. Warning, it's untested.

int waysBin(int num_balls, BinState binState) {

    int ways = 0;

    for (int i = 0; i < binState.numberofbins(); i++) {
        BinState deepCopyOfBinState = ?;
        /* I don't know how to make a deep copy in java.
         You will need to make a deep copy because this 
         function modifies the deep copy (adding a ball)
         */
        Bin indexedBin = deepCopyOfBinState.getBinNumber(i);
        if (indexedBin.isUnderCapacity()) {
            indexedBin.addOneBall();
            ways += waysBin(num_balls-1, deepCopyOfBinState);
        } else {
            ways += 1; // maybe
        }

    }

    return ways;
}

Then you call the recursive function from the function you wrote above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM