Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9116103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:35:28+00:00 2026-06-17T04:35:28+00:00

Let’s say i have some items, that have a defined length and horizontal position

  • 0

Let’s say i have some items, that have a defined length and horizontal position (both are constant) :

1 : A      
2 :     B
3 :  CC
4 :  DDD   (item 4 start at position 1, length = 3)
5 :    EE  
6 : F

I’d like to pack them vertically, resulting in a rectangle having smallest height as possible.

Until now, I have some very simple algorithm that loops over the items and that check row by row if placing them in that row is possible (that means without colliding with something else). Sometimes, it works perfectly (by chance) but sometimes, it results in non-optimal solution.

Here is what it would give for the above example (step by step) :

 A      |  A   B  |  ACC B  |  ACC B  |  ACC B  |  ACC B  | 
                                DDD   |   DDD   |  FDDD   |
                                            EE  |     EE  |

While optimal solution would be :

ADDDB 
FCCEE

Note : I have found that sorting items by their length (descending order) first, before applying algorithm, give better results (but it is still not perfect).

Is there any algorithm that would give me optimal solution in reasonable time ? (trying all possibilities is not feasible)


EDIT : here is an example that would not work using sorting trick and that would not work using what TylerOhlsen suggested (unless i dont understand his answer) :

1 : AA
2 :    BBB
3 :   CCC 
4 :  DD 

Would give :

AA BBB
  CCC 
 DD

Optimal solution :

 DDBBB
AACCC 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:35:29+00:00Added an answer on June 17, 2026 at 4:35 am

    Just spitballing (off the top of my head and just pseudocode ). This algorithm is looping through positions of the current row and attempts to find the best item to place at the position and then moves on to the next row when this row completes. The algorithm completes when all items are used.

    The key to the performance of this algorithm is creating an efficient method which finds the longest item at a specific position. This could be done by creating a dictionary (or hash table) of: key=positions, value=sorted list of items at that position (sorted by length descending). Then finding the longest item at a position is as simple as looking up the list of items by position from that hash table and popping the top item off that list.

    int cursorRow = 0;
    int cursorPosition = 0;
    int maxRowLength = 5;
    
    List<Item> items = //fill with item list
    Item[][] result = new Item[][];
    
    while (items.Count() > 0)
    (
        Item item = FindLongestItemAtPosition(cursorPosition);
        if (item != null)
        {
            result[cursorRow][cursorPosition] = item;
            items.Remove(item);
            cursorPosition += item.Length;
        }
        else //No items remain with this position
        {
            cursorPosition++;
        }
    
        if (cursorPosition == maxRowLength)
        {
            cursorPosition = 0;
            cursorRow++;
        }
    }
    

    This should result in the following steps for Example 1 (at the beginning of each loop)…

     Row=0  |  Row=0  |  Row=0  |  Row=1  |  Row=1  |  Row=1  |  Row=2  |
     Pos=0  |  Pos=1  |  Pos=4  |  Pos=0  |  Pos=1  |  Pos=3  |  Pos=0  |
    
            |  A      |  ADDD   |  ADDDB  |  ADDDB  |  ADDDB  |  ADDDB  | 
                                             F      |  FCC    |  FCCEE  |
    

    This should result in the following steps for Example 2 (at the beginning of each loop)…

     Row=0  |  Row=0   |  Row=0   |  Row=1   |  Row=1   |  Row=1   |  Row=2   |
     Pos=0  |  Pos=2   |  Pos=4   |  Pos=0   |  Pos=1   |  Pos=3   |  Pos=0   |
    
            |  AA      |  AACCC   |  AACCC   |  AACCC   |  AACCC   |  AACCC   |
                                                             DD    |   DDBBB  |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have two text files that I need to extract data out
Let's say for a moment that I have the following module in python: class
Let's say you have a class library project that has any number of supplemental
Let's say I have an abstract parent class called shape, and that there are
Let say I have my view that I use it as a toggle button.
Let's say, I have an application that access(read/write) the file system(files inside application), Active
Let's say I have some data in a CouchDB database. The overall size is
Let's say that I have a html form (actually I have an editor -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.