BudiBadu Logo

Library Shelf Merge Log

Sorting Easy 5 views
Like27

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

Example 1
Input
shelves = [[1,4],[2,3]], merges = []
Output
[[1,4],[2,3]]
Explanation

With no merges scheduled, the layout stays intact.

Example 2
Input
shelves = [[2,4],[1,3,5],[6]], merges = [[0,1],[0,1]]
Output
[[[1,2,3,4,5,6]]]
Explanation

First merge shelves 0 and 1 into [1,2,3,4,5], then merge that shelf with the final shelf [6].

Example 3
Input
shelves = [[10],[8,12],[9]], merges = [[1,2]]
Output
[[10],[8,9,12]]
Explanation

Only shelf index 1 and 2 are merged; shelf 0 remains unchanged.

Algorithm Flow

Recommendation Algorithm Flow for Library Shelf Merge Log - Budibadu
Recommendation Algorithm Flow for Library Shelf Merge Log - Budibadu

Best Answers

java
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][]);
    }
}