PracticeArrays

Combine Sorted Warehouse Dispatch Lists

EasyArraystraversalstate trackinginvariants Time not estimated Not started

Context

A logistics hub receives two dispatch lists from separate warehouses. Each list is already ordered by package priority number, from smallest to largest. The hub needs one combined dispatch list that keeps the same ordering and includes every package entry, even when several packages share a priority number.

Problem

Implement a function that combines two nondecreasing integer arrays into one nondecreasing array. Every element from both input arrays must appear in the returned array exactly as many times as it appears in its source array. If one array has no remaining elements, continue taking elements from the other array. Equal values must not be discarded. Use the ordering already present in the inputs rather than treating the data as unsorted.

Examples

Example 1
Input: warehouse_a = [1, 4, 7]warehouse_b = [2, 4, 6]
Output: [1, 2, 4, 4, 6, 7]
Explanation: For [1, 4, 7] and [2, 4, 6], the executed output is [1, 2, 4, 4, 6, 7], preserving both occurrences of 4.
Example 2
Input: warehouse_a = [-5, -2, 0, 0, 3]warehouse_b = [-4, -2, 1]
Output: [-5, -4, -2, -2, 0, 0, 1, 3]
Explanation: For [-5, -2, 0, 0, 3] and [-4, -2, 1], the executed output is [-5, -4, -2, -2, 0, 0, 1, 3], preserving both occurrences of -2 and both occurrences of 0.
Example 3
Input: warehouse_a = [2, 2, 8]warehouse_b = []
Output: [2, 2, 8]
Explanation: For [2, 2, 8] and an empty array, the executed output is [2, 2, 8].

Constraints

  • 0 <= len(warehouse_a) <= 100000
  • -1000000000 <= warehouse_a[i] <= 1000000000 for every i
  • warehouse_a: sorted non decreasing
  • 0 <= len(warehouse_b) <= 100000
  • -1000000000 <= warehouse_b[i] <= 1000000000 for every i
  • warehouse_b: sorted non decreasing
  • Types: warehouse_a is int[], warehouse_b is int[]; result is int[]

Function signature

def merge_dispatch_lists(warehouse_a: list[int], warehouse_b: list[int]) -> list[int]
warehouse_a list[int]
The first warehouse's dispatch priorities in nondecreasing order.
warehouse_b list[int]
The second warehouse's dispatch priorities in nondecreasing order.
returns list[int]
A new array containing the multiset union of both inputs in nondecreasing order, including all duplicate occurrences.

Adapted from MBPP problem task_943 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Approved · t3-abs_343_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1