BudiBadu Logo

Harbor Container Shuffle Plan

Sorting Easy 6 views
Like14

The harbor’s night crew rearranges shipping containers based on Shuffle Plans. Each plan specifies a range [start, end] in the yard that must be sorted into ascending order. Your mission in Harbor Container Shuffle Plan is to return the final container order after all plans have been executed sequentially.

The "secret sauce" here is In-Place Segment Sorting. You're given the initial list of containers and a sequence of plans. For each plan, you sort the containers within that specific range (inclusive) while leaving everything else in its current spot. Plans must be applied one by one, mirroring the crew’s written checklist. This simulation allows the operations team to confirm bays are tidy before the morning cranes arrive. It’s a great way to practice array slicing and sequence management under real-world constraints!

If sorting is part of the strategy, do it intentionally as a preprocessing step to simplify downstream logic such as merging, ordering, or comparison. After sorting, keep output semantics precise: preserve expected structure, avoid dropping valid entries, and ensure tied cases still follow deterministic order rules.

Examples

Example 1
Input
containers = [5,8,2], plans = []
Output
[5,8,2]
Explanation

With no shuffle plans, the yard remains unchanged.

Example 2
Input
containers = [7,3,5,2,9], plans = [[0,2],[1,4]]
Output
[3,2,5,7,9]
Explanation

First sort the prefix [7,3,5] -> [3,5,7], producing [3,5,7,2,9]; next sort indices 1-4 to get [3,2,5,7,9].

Example 3
Input
containers = [4,1,6,3], plans = [[0,3],[0,1]]
Output
[1,3,4,6]
Explanation

Sorting the entire row yields [1,3,4,6]; the second command keeps the first two elements in order.

Algorithm Flow

Recommendation Algorithm Flow for Harbor Container Shuffle Plan - Budibadu
Recommendation Algorithm Flow for Harbor Container Shuffle Plan - Budibadu

Best Answers

java
import java.util.*;

class Solution {
    public int[] harbor_container_shuffle_plan(int[] containers, int[][] plans) {
        for (int[] plan : plans) {
            Arrays.sort(containers, plan[0], plan[1] + 1);
        }
        return containers;
    }
}