Library Shelf Merge Log
The library curator is merging book logs from two branches to create a unified master list. In Library Shelf Merge Log, your task is to combine two arrays of IDs into a single list sorted in ascending order from smallest to largest. This ensures the morning readers find their books perfectly organized!
The "secret sauce" is Multi-Source Sorting. You receive separate lists, combine all entries, and sort the results. Your routine must preserve every ID, including duplicates (titles owned by both branches) and negative placeholders (books out for repair). The original branch logs must remain untouched, so return a brand-new unified list. This simple merge logic turns messy logs into a professional, searchable index that keeps the intake flow running smoothly and predictably for the morning shift!
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
With no merges scheduled, the layout stays intact.
First merge shelves 0 and 1 into [1,2,3,4,5], then merge that shelf with the final shelf [6].
Only shelf index 1 and 2 are merged; shelf 0 remains unchanged.
Algorithm Flow

Best Answers
import java.util.*;
class Solution {
public Object library_shelf_merge_log(Object input) {
Object[] args = (Object[]) input;
int[][] shelves_arr = (int[][]) args[0];
int[][] merges = (int[][]) args[1];
List<List<Integer>> s = new ArrayList<>();
for (int[] sh : shelves_arr) {
List<Integer> list = new ArrayList<>();
for (int x : sh) list.add(x);
s.add(list);
}
Set<Integer> to_remove = new HashSet<>();
for (int k = merges.length - 1; k >= 0; k--) {
int i = merges[k][0], j = merges[k][1];
s.get(i).addAll(s.get(j));
Collections.sort(s.get(i));
to_remove.add(j);
}
List<int[]> resList = new ArrayList<>();
for (int idx = 0; idx < s.size(); idx++) {
if (!to_remove.contains(idx)) {
List<Integer> list = s.get(idx);
int[] row = new int[list.size()];
for (int m_idx = 0; m_idx < list.size(); m_idx++) row[m_idx] = list.get(m_idx);
resList.add(row);
}
}
return resList.toArray(new int[0][]);
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
