Does my opinion matter? You are using a greedy approach for an NP complete problem (knapsack). So you don't get an optimal result (where optimal is, I presume, the use of a minimal number of lines), but you get it pretty quickly. Whether that's good enough, I don't know. Only you, and the enduser (or client), can decide that.
Now, if you have thousands of fragments, with many fragments on each line, you might want to use a segment tree for each of the lines you create, to be able to determine faster whether a new fragment fits a line.
Alternatively, you chould sort the fragments on starting position. Then, do something like:
This is also greedy; it won't find the optimal packing, but it's reasonbly fast.0. Create a new empty line, and make it the current one. 1. Find the first fragment in the list that fits on the line. 2. If there is no such fragment, goto 0. 3. Add the fragment to the current line, and remove it from the list. 4. If the list is empty, you're done. 5. Goto 1.
In reply to Re: formatting output question (use of recursive subroutine?)
by JavaFan
in thread formatting output question (use of recursive subroutine?)
by rogerd
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |