K ordered array, find the smallest interval range so that the K arrays, each array has at least one number within the range of this interval. Like what:
The minimum interval is [20, 24], which contains 24 in array 1, 2 in array 20, 3 in array 22
Analysis
The problem seems relatively simple, it is common to think: set a traversal variable for each array, select the minimum value of the array, continue to move a bit. Since there is a K array, the number of arrays can be many, so how to choose and replace the smallest value, we will think of a data structure minimum heap to maintain the minimum value.
Answer method:
Initialize the smallest heap of size k, the K number is the smallest value in each array, set the variable MaxValue record the maximum value in K number, delete the top element of the heap, add the next value in the array of the top element of the original heap to the heap, adjust the heap, and record the current interval range (MaxValue- MinValue), which is repeated until all values of an array are deleted.
Like what.
List 1: [4, 10, 15, 24, 26]
List 2: [0, 9, 12, 20]
List 3: [5, 18, 22, 30]
The minimum heap size is 3. Take minimum values from three arrays
Heap [0, 4, 5] MaxValue 5
Range-6
Delete 0, add 9
Heap [4, 9, 5] MaxValue 9
Range-6
Delete 4, add 10
Heap [5, 9, ten] MaxValue 10
Range-6
Repeated execution, resulting in results
The code is as follows:
1 structPN2 {3 intN/*belong to which array*/4 intD/*The data value*/5Pnint_n,int_d) {n = _n; d =_d;}6PnConstpn& _pn) {n = _pn.n; d =_PN.D;}7 };8 9InlinevoidSwap (pn& A, pn& b) {pn c = A; a = b; b =C;}Ten One voidAdjustintN, PN a[]) A { - inti =0, max =0; - intL =0, r =0; the for(i = n/2; I >=0; i--) - { -Max =i; -L =2* i +1; +R =2* i +2; - if(L < n && a[l].d > A[max].d) {max =l;} + if(R < n && a[r].d > A[max].d) {max =R;} A if(max! =i) {swap (A[max], a[i]);} at } - } - - voidHeapsort (intN, PN a[]) - { - inti =0; in adjust (n, a); - for(i = n-1; i >0; i--) to { +Swap (a[0], a[i]); - adjust (i, a); the } * } $ Panax Notoginseng intMain () - { the inti =0, j =0; + Const intm =3; A Const intn =5; the intms =0, ME =0; + intTS =0, TE =0; - intA[m][n] = {{4,Ten, the, -, -}, {0,9, A, -, *}, {5, -, A, -, -} }; $ intCur[m] = {1,1,1};/*Record the current positions of each array which haven ' t been used*/ $PN Heap[m] = {PN (0, a[0][0]), PN (1, a[1][0]), PN (2, a[2][0])}; - - heapsort (M, heap); thems = Heap[0].d; -me = heap[m-1].d;Wuyi while(true) the { - heapsort (M, heap); WuTS = heap[0].d; -Te = heap[m-1].d; About /*if the current range is smaller than the minimum range*/ $ if(Te-ts < Me-ms) {ms = ts; me =te;} - - /*if the sub-array which the smallest element comes from hasn ' t to the end*/ - if(cur[heap[0].N]! =N) A { +heap[0].D = a[heap[0].n][cur[heap[0].N]]; thecur[heap[0].N] + =1; - } $ Else the { the Break; the } the } -cout << Ms <<Endl; incout << Me <<Endl; the return 0; the}
Here's your idea:
1 Each array takes the last value, from which the minimum value is selected, recorded as Min.
2 iterates through each array (min corresponds to the return-1), finds the first value greater than Min, and then selects the maximum value from the values.
3 The minimum interval is [min, Max].
Don't know if there's a loophole? Implemented later.
Turn: Minimum interval: K ordered array, find the minimum interval so that each array in the K group has at least one number in the interval