Double the Median When Merging Two Sorted Price Lists

Implement doubledMedianPrice

Given two lists of prices, prices1 and prices2, each already sorted in non-decreasing order (possibly empty, but never both at once), imagine them merged into one combined sorted list without actually building it. Return double the median of that combined list — doubling keeps the answer a whole number even when the true median would otherwise be a fraction (the average of two middle values), so the function never needs to return anything other than an integer. The direct approach merges the two lists first (an easy O(m+n) two-pointer walk, since both are already sorted) and then reads off the middle value or values. That works, but it does far more work than necessary just to find one or two positions near the middle. The faster approach never builds the merged list at all: it binary searches directly for the partition pointBinary Search on the PartitionInstead of searching over array values or over a range of candidate answers, this binary search hunts for a cut position (an index) in the shorter array. A cut is valid once every value kept on its left side is ≤ every value kept on its right side across both arrays combined — exactly the ordering a real merge would produce, found without ever performing the merge. — the single cut position in the shorter list that splits both lists' combined values cleanly into a "left half" and a "right half," with every left value ≤ every right value. Once that cut is found, the median (doubled) is read directly off its boundary values, in O(log(min(m, n))) time.

Example 1:

Input: prices1 = [12,45], prices2 = [30]

Output: 60

Example 2:

Input: prices1 = [8,19], prices2 = [15,26]

Output: 34

Example 3:

Input: prices1 = [], prices2 = [22]

Output: 44

+ 10 hidden test cases run on Submit.

Constraints:

  • 0 ≤ length of prices1 ≤ 1000
  • 0 ≤ length of prices2 ≤ 1000
  • prices1 and prices2 are never both empty
  • -10⁶ ≤ prices1[i], prices2[i] ≤ 10⁶
  • Both prices1 and prices2 are sorted in non-decreasing order

prices1 =

[12, 45]

prices2 =

[30]