Code Logo

Warehouse Crate Ordering

Published at05 Jan 2026
Easy 4 views
Like13

This problem feels like a little puzzle you can solve one step at a time. In Warehouse Crate Ordering, you are trying to work toward the right list by following one clear idea.

You are given numbers, words, or labels that are not in the right order yet. Your task is to rearrange them so they line up the way the problem wants. That usually means comparing values carefully and making sure nothing gets lost or changed by mistake. After you finish, the result should feel neat and easy to read.

For example, if the input is stacks = [["M","N"],["P"]], moves = [], the answer is [["M","N"],["P"]]. No commands means the stacks remain unchanged. Another example is stacks = [["X"],["Y","Z"]], moves = [[1,0,3]], which gives [["X","Y","Z"],[]]. Command asks for three crates but stack 1 has only two, so both move onto stack 0.

This is a friendly practice problem, but it still rewards careful reading. Keep your eye on the order from start to finish, and make sure the final list is exactly how the question wants it.

Example Input & Output

Example 1
Input
stacks = [["M","N"],["P"]], moves = []
Output
[["M","N"],["P"]]
Explanation

No commands means the stacks remain unchanged.

Example 2
Input
stacks = [["X"],["Y","Z"]], moves = [[1,0,3]]
Output
[["X","Y","Z"],[]]
Explanation

Command asks for three crates but stack 1 has only two, so both move onto stack 0.

Example 3
Input
stacks = [["A","B"],["C"],["D","E","F"]], moves = [[2,0,2],[0,1,1]]
Output
[["A","B","E","F"],["C","D"],[]]
Explanation

Move two crates from stack 2 onto stack 0 (E,F), then move one crate from stack 0 onto stack 1 (now D on top of C).

Algorithm Flow

Recommendation Algorithm Flow for Warehouse Crate Ordering
Recommendation Algorithm Flow for Warehouse Crate Ordering

Solution Approach

A good way to solve this problem is to simulate the stacks directly. Each inner array already acts like a stack, where the last element is the top crate. For every move instruction, you repeatedly pop crates from the source stack and push them onto the destination stack until you either move the requested amount or the source stack becomes empty.

The important detail is that moves happen from the top of the source stack. That means you always remove the last element of the source array. If the instruction asks for more crates than the source actually has, you simply move everything available and stop. The examples make that behavior explicit, especially the case where a move asks for three crates but only two exist.

In JavaScript, the core simulation can be written like this:

for (const [from, to, count] of moves) {
  let moved = 0;
  while (moved < count && stacks[from].length > 0) {
    const crate = stacks[from].pop();
    stacks[to].push(crate);
    moved++;
  }
}
return stacks;

This mirrors the problem statement closely. The outer loop processes instructions in order, which matters because every move changes the stack layout for the next instruction. The inner loop handles one crate at a time so the top-of-stack behavior stays correct.

That one-at-a-time movement also explains the ordering in the result. If you move multiple crates from one stack to another, the top crate moves first, then the next one, and so on. Because each moved crate is pushed onto the destination stack immediately, the moved group arrives in reversed pop order, which is exactly what a normal stack transfer does.

This approach handles empty move lists, empty source stacks, and oversized move counts naturally. If there are no moves, the stacks stay unchanged. If a source stack is empty, the inner loop never runs. If the requested count is larger than the available crates, the move just stops when the source becomes empty.

The total running time is proportional to the number of individual crate moves actually performed. Extra space stays O(1) beyond the stacks themselves because you only use a few loop variables.

So the full strategy is: treat each inner array as a stack, pop from the source, push onto the destination, follow the instructions in order, and stop each move when either the requested count is reached or the source stack runs out.

Best Answers

java
import java.util.*;
class Solution {
    public int[] sort_crates(int[] nums) {
        int[] res = nums.clone(); Arrays.sort(res); return res;
    }
}