good chemistry is complicated, and a little bit messy LW 

PerlMonks 
Comment on 
( #3333=superdoc: print w/ replies, xml )  Need Help?? 
O.K., I may have misread this problem at first because "height" is somewhat ambigous. If "height" somehow refers to the aggregation of the data, here's another approach.
Given the constraints, I count 10 possible groupings: So my approach two is to brute force sum the values for each possible combination, and select the one that best meets the criteria about "height". Then, assuming that works, I'd generalize an algorithm for generating groupings given number of categories and number of columns. Like the one fglock provides above. Is this what you're looking for, or does "height" refer to the number of rows in the table?
In reply to Re: Puzzle: need a more general algorithm
by dws

