The abstraction of my problem is that in a cartesian plane there are a lot of rectangles. These rectangles have known integer sizes and must have integer coordinates their abscissas (horizontal coordinates) are known and fixed, only their ordinates (vertical coordinates) may vary.
The problem is to find those ordinates for which the smallest rectangle that contains all the given rectangles is minimum. This means that it should have minimum height, since its width is fixed because the small rectangles have fixed abscissas.
I don't know if i should use backtracking or there is a faster way, i can imagine that at 50 rectangles it would take some measurable time to compute the correct solution and a greedy algorithm doesn't sit well with me.
Edit: I'm sorry, I now realize I wasn't clear enough. When I first asked this question I was building a calendar application. A manager would fill in events for his team:
- event A starts from 2p.m. and ends at 4p.m.
- event B starts from 5p.m. and ends at 6p.m.
- event C starts from 4p.m. and ends at 6p.m.
- event D starts from 2p.m. and ends at 3p.m.
- event E starts from 3p.m. and ends at 5p.m.
I want to display these events on a timeline and I want them to take as little screen estate as possible, without overlapping(because the manager wants to see each event in its rectangle, and to see a description in that rectangle).
The best arrangement for the example above would be the following:
+-----+-----+
| A | C |
+---+-+-+---+
| D | E | B |
+---+---+---+
A and C are on a line, D, E, B are on another. A greedy approach would have put A and B on the same line, C and D on another line, and E on a third line.