How can I fill bookcases with shelves of books using the least number of bookcases?
I'm not clear on whether you have one bookcase or multiple bookcases, so I'll explain how to handle both cases below.

Multiple bookcases

If you have multiple bookcases, and you need to put each shelf into exactly one of the bookcases, then you have an instance of the bin packing problem.

Each shelf corresponds to an item, and each bookcase corresponds to a bin. Now you want to find a way to put each item into one bin, i.e., put each shelf into one bookcase, so that every shelf is in some bookcase, and each bookcase has room for all the shelves that will go into it.

This can be modelled in a straightforward way as a bin packing problem:


The "size" of a shelf (an item) is the height of the shelf.
The "size" of a bookcase (i.e., of a bin) is the height of the bookcase.


Now you want to find a way to assign each shelf to one of the bookcases, so that the sum of the sizes (heights) of the shelves in each bookcase does not exceed the size (height) of the bookcase.  That is exactly the bin packing problem.

You should be able to use any standard algorithm for bin packing to solve your problem.  Given the number of bookcases and shelves you're likely to have in practice, any of them should be efficient enough for practical use.  For instance, one approach is to formulate this as an instance of ILP (integer linear programming), as described in the Wikipedia article on bin packing, and then solve the problem using an off-the-shelf ILP solver, such as lp_solve or CPLEX.  This will likely be very effective in practice.

All of these algorithms can accommodate the requirement to put the tallest shelf on the top: you select the set of shelves to go into each bookcase, and then for each bookcase, find the tallest of the shelves that are going into that bookcase and put it at the top of the bookcase.

One bookcase

If you have one bookcase, and you can select some subset of the shelves to fill the bookcase, your problem is an instance of the knapsack problem.

Treat each shelf as an item that can be placed into the knapsack.  Then:


the "weight" of a shelf is its height,
the "value" of a shelf is the value of including that shelf in the bookcase (e.g., the number of books it'll enable you to store, or maybe just a fixed value that's the same for all shelves), and
the "capacity" of the knapsack is the total height of the bookcase.


The optimal solution to the knapsack problem will select a subset of items whose total weight doesn't exceed the knapsack's capacity, and that maximizes the sum of the values of the selected items.

In your context, that amounts to selecting a subset of the shelves whose total height doesn't exceed the height of the bookcase (and thus it'll be possible to fit all of the selected shelves into your bookcase), and that maximizes the sum of the values of the shelves (e.g., maximizes the total number of shelves that you fit into the bookcase, or the total number of books you can store in the bookcase, etc.).

There are many algorithms for the knapsack problem, such as the dynamic programming algorithm.  In your situation, I suspect the number of candidate shelves is small enough that any of these algorithms is likely to be efficient enough for real-world use.

All of these algorithms can accommodate the requirement to put the tallest shelf on the top: you select the set of shelves to go into the bookcase, and then choose the one that is tallest and put it at the top of the bookcase.