1513. Kth Combined Price
A gift shop sells n mugs and m plates. The array a holds the mug prices and the array b holds the plate prices. A customer buys exactly one mug and one plate, so there are n times m possible bundles, each costing the mug price plus the plate price. Bundles made from different items are different bundles even when their costs are equal, and prices may be negative because of coupons.
Imagine all bundle costs sorted from smallest to largest with repeats kept. Return the cost at position k, counting from 1, as a 64-bit integer. Only the cost is returned, so ties need no extra rule. Aim for O((n + m) log(range)) time after sorting, and O(n + m) extra space.
Example 1
- Input:
- a = [1,4], b = [2,3], k = 3
- Output:
- 6
- Explanation:
The four costs are 3, 4, 6, 7; the 3rd smallest is 6.
Example 2
- Input:
- a = [5], b = [-2,-2,9], k = 2
- Output:
- 3
- Explanation:
Costs are 3, 3 and 14, so the 2nd smallest is 3.
Example 3
- Input:
- a = [-1,0,1], b = [-1,0,1], k = 5
- Output:
- 0
- Explanation:
The nine costs sorted are -2, -1, -1, 0, 0, 0, 1, 1, 2; the 5th is 0.
Constraints
1 ≤ n, m ≤ 105
-109 ≤ a[i], b[j] ≤ 109
1 ≤ k ≤ n*m
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 10,000 msC++ 2,500 msJava 5,000 msJavaScript 5,000 msTypeScript 5,000 ms
Expected complexity
- Time
- O((n + m) log R)
- Space
- O(n + m)