Kth smallest Sum in Sorted Arrays

Source: Internet
Author: User

Given-arrays sorted in ascending order and an integer k. Define sum = a + b, where a was an E Lement from the first array and B are an element from the second one. Find the kth smallest sum out of all possible sums.

Given [1, 7, 11] and [2, 4, 6] .

For k = 3 , return 7 .

For k = 4 , return 9 .

For k = 8 , return 15 .

This is the follow up of the kth smallest number in the Sorted matrix. It may be hard to think at first, but the sums of these two arrays are paired with all elements of the second array if the first array of the first array is paired with all the elements of the other. The result is a sort matrix. So you can completely take the previous heap idea to do. However, if you use the previous idea directly to do the memory, we reflect on the previous approach. For matrices, the length of the width is not particularly large. But for arrays, the lengths can be very long, or even much larger than the size of K. So at this time, it is completely unnecessary to deal with the matrix of K-row and K-column. The size of the visited matrix can be limited to min (K,len (Matrix)) and Min (k , Len (Matrix[0])). greatly reduced space. The space complexity is O (min (k,m) *min (K,n)). The time complexity is O (K*log (m,n,k)). The code is as follows:

classSolution:#@param {int[]} A integer arrays sorted in ascending order    #@param {int[]} B An integer arrays sorted in ascending order    #@param {int} k an integer    #@return {int} an integer    defkthsmallestsum (self, A, B, K): a=Len (A) b=Len (B) fromHeapqImport*Heap= [(a[0]+b[0], (0,0))] Visited= [[False] * min (b,k) forJinchxrange (min (a,k))] I=0 whileI <K:cur, (x, y)=Heappop (heap) I+ = 1ifX + 1 < min (A, k) andVisited[x+1][y] = =False:heappush (Heap, (A[x+1] + b[y], (x+1, y))) Visited[x+1][y] =TrueifY + 1 < min (b, k) andVISITED[X][Y+1] = =False:heappush (Heap, (a[x)+ b[y+1], (x, y+1)) Visited[x][y+1] =TruereturnCur

Another way to do this is to put the first row of elements in the heap, and then update each face down, so as to avoid the duplication of the right and down, without maintaining the visited matrix and making complex boundary judgments. The code references the following nine chapters:

classSolution:#@param {int[]} A integer arrays sorted in ascending order    #@param {int[]} B An integer arrays sorted in ascending order    #@param {int} k an integer    #@return {int} an integer    defkthsmallestsum (self, A, B, K):#Write Your code here        if  notAor  notB:return0ImportHEAPQ m, n=Len (A), Len (B)defVertical_search (k): Heap= []             forIinchRange (min (k, n)): Heapq.heappush (Heap, (a[0 )+B[i], 0, i))  whileK > 1: Min_element=Heapq.heappop (heap) x, y= Min_element[1], min_element[2]                ifX + 1 <M:heapq.heappush (Heap, (A[x+ 1] + b[y], X + 1, y)) K-= 1returnHeapq.heappop (heap) [0]returnVertical_search (k)

Kth smallest Sum in Sorted Arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.