Box stacking problem

后端 未结 5 1830
灰色年华
灰色年华 2020-11-28 20:01

I found this famous dp problem in many places, but I can not figure out how to solve.

You are given a set of n types of rectangular 3-D boxes, where

5条回答
  •  清酒与你
    2020-11-28 20:35

    A solution to the problem consists of three steps.

    1. Sort dimensions for each box so that comparing any two boxes reduces to comparing their corresponding dimensions.
    2. Sort the sequence of boxes lexicographically so that for each box, the boxes to the left are the boxes that may fit.
    3. Apply the O(n^2) algorithm for the Longest Increasing Subsequence Problem.

    The third step is the most expensive and bumps the complexity of the solution to O(n^2). If you would like to read a complete explanation of the approach, how each step contributes to finding an answer, and full code, have a look at the blog post I wrote about the problem.

提交回复
热议问题