Greedy algorithm implementation, Haskell -
i need implement haskell function receives 1 int (truck load capacity), , list of ints (boxes models can loaded on truck).
to define box models must placed preferably in truck, it's requested boxes greater capacity in relation available space, placed first.
the algorithm should return list of models of boxes placed on truck. have no idea how program functional paradigm :/
maximizeload 103 [15, 20, 5, 45, 34] [45, 45, 5, 5]
thanks!
bruce force approach smart filtering
maximumload n = head . head . group length . last . group sum . filter ((<= n) . sum) . map concat . sequence . map (rep n) . reverse . sort rep n x = take ((div n x)+1) $ iterate (x:) [] group f = groupby ((==) `on` f) . sortby (comparing f) > maximumload 103 [15, 20, 5, 45, 34] [34,34,20,15]
update greedy algorithm, simpler. think code easy read describe algorithm. expects reverse sorted input list.
maxload _ [] = [] maxload n (x:xs) | n==0 = [] | x <= n = x: maxload (n-x) (x:xs) | otherwise = maxload n xs > maxload 103 $ reverse $ sort [15, 20, 5, 45, 34] [45,45,5,5]
Comments
Post a Comment