Kth Smallest Sum In Two Sorted Arrays
Difficulty Hard Accepted Rate 19%
Question
Given two integer arrays sorted in ascending order and an integer k. Define sum = a + b, where a is an element from the first array and b is an element from the second one. Find the kth smallest sum out of all possible sums.
Example
Given [1, 7, 11] and [2, 4, 6].
For k = 3, return 7.
For k = 4, return 9.
For k = 8, return 15.
Challenge Do it in either of the following time complexity:
O(k log min(n, m, k)). where n is the size of A, and m is the size of B. O( (m + n) log maxValue). where maxValue is the max number in A and B.
Tags
Heap Priority Queue Sorted Matrix
Related Problems
Medium Kth Smallest Number in Sorted Matrix Medium Search a 2D Matrix II
Analysis
此题乍看似乎没有很好的思路,其实稍加转化就变成了熟悉的问题:Kth Smallest Number in Sorted Matrix.
两个array中间分别取一个数相加得到一个sum,其实可以想象一个Matrix,里面元素的坐标就是分别在两个Array中的下标index,而元素的值则是sum的值。那么很显然,因为两个array都是排序过的,那么对于这个想象中的matrix中的每一行每一列来说,都是排好序的。
比如对于[1, 7, 11] and [2, 4, 6].
接下来就可以利用PriorityQueue构造Min Heap来解决了。
Solution
Reference
Last updated
Was this helpful?