Bin Packing Problem ( Minimize number of used Bins ) | Returns number of bins required using first fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the first bin that can accommodate weight [ i ] ; If no bin could accommodate weight [ i ] ; Driver program
def firstFit ( weight , n , c ) : NEW_LINE INDENT res = 0 NEW_LINE bin_rem = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT j = 0 NEW_LINE while ( j < res ) : NEW_LINE INDENT if ( bin_rem [ j ] >= weight [ i ] ) : NEW_LINE INDENT bin_rem [ j ] = bin_rem [ j ] - weight [ i ] NEW_LINE break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( j == res ) : NEW_LINE INDENT bin_rem [ res ] = c - weight [ i ] NEW_LINE res = res + 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT weight = [ 2 , 5 , 4 , 7 , 1 , 3 , 8 ] NEW_LINE c = 10 NEW_LINE n = len ( weight ) NEW_LINE print ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ First ▁ Fit ▁ : ▁ " , firstFit ( weight , n , c ) ) NEW_LINE