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
function firstFit ( weight , n , c ) { let res = 0 ; let bin_rem = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { let j ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] ) { bin_rem [ j ] = bin_rem [ j ] - weight [ i ] ; break ; } } if ( j == res ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } } return res ; } let weight = [ 2 , 5 , 4 , 7 , 1 , 3 , 8 ] ; let c = 10 ; let n = weight . length ; document . write ( " " + firstFit ( weight , n , c ) ) ;