Question

enter image description here

I'm a little stuck trying to solve this problem. The main source of confusion comes from not knowing when to remove a box.

Here's my approach:

I look at the container column by column. If the top most box of the origin box is empty and the destination box is not empty, then i know to add that box. And i know to remove the top box if its vice versa. I think that I have to swap when both positions have a box but are different. However my problem comes with certain cases where removing a box at the bottom will shift everything down and make it more like the destination box. Or maybe removing one in the center or removing two, one in the bottom and one in the center. How do I know when to remove a box? I can remove all combinations and see which makes it most close to the destination but that doesn't seem efficient.

I also maybe think that this is an obvious dynamic programming problem thats going over my head. Any help would be appreciated

Was it helpful?

Solution

You have already reduced the problem to considering one column at a time, so let us start with that.

Instead of considering particular operations that can happen in a column, let us look at the process in general. Initially, we have the given column. At the end, we have the resulting column. What's left of the given column in the resulting column? It is a subsequence (since we can remove a box from anywhere) of the given column which transferred to a prefix of the resulting column ("prefix" as in "located at the bottom", since we can add new boxes only on top of what was there initially).

Naturally, we want to maximize the length of this sequence (subsequence or prefix, depending on where you look at). That sure looks like a dynamic programming problem, similar to edit distance or longest common subsequence. Perhaps you will want to work out the details yourself from this point. Good luck!

OTHER TIPS

You can of course work one column at a time.

Then to transform column [x1, x2, x3, ...] into column [y1, y2, y3, ...] you have a few cases:

  • case (A): x1 is the same as y1: this is the easy case, you need to match the rest
  • case (B): x1 is - and y1 is not: you need to insert all remaining y boxes
  • case (C): y1 is - and x1 is not: you need to remove all remaining x boxes
  • case (D): x1 and y1 are not empty but different; here you have to choose between: (D1) flip x1 and match [x2,...] with [y2,...], (D2) remove x1 and match [x2, ...] with [y1, ...]. You should choose (D1) or (D2) depending on which one requires less operations.

Note: the option (D3) of inserting y1 in x and matching [x1,...] with [y2,...] is not available given the rules because you can only add boxes at the top of the pile (case B).

This translates in a dynamic programming (or recursion with memoizing) algorithm:

int min_moves(int i, int j);

and you need to compute the number of moves to align column x[i], x[i+1], ... to column y[j], y[j+1], ... where x and y are the original contents of the two manifests read from the file, assuming x[i] and y[i] to be - for any i>m.

To compute min_moves(i, j) you can use min_moves(i+1, j+1) (cases A+D1), min_moves(i+1, j) (case D2). Cases B and C require no recursion but are a direct computation on x and y.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top