Algorithm-calculate the minimum K Number and k Number of values added to two ordered arrays
My thinking is:
Use a queue to start from (0, 0). When you leave the queue, select the smallest number (1, 0) (0, 1). If the number is equal, if you enter the team, you will not be able to enter the team. Just output k different outputs.
I tested several groups of data that are correct, but there may still be bugs, or something I ignored. The following is my implementation code (if there are any mistakes, please correct them)
Import java. util. using list; import java. util. queue;/*** there are two sequences A and B, A = (a1, a2 ,..., ak), B = (b1, b2 ,..., bk), A and B are arranged in ascending order. For 1 <= I, j <= k, k is the smallest (ai + bj ), the algorithm should be as efficient as possible * @ author Administrator **/public class Test {int k = 4; int a [] = new int [] {1, 2, 4 }; int B [] = new int [] {20, 30, 40, 50}; boolean visited [] = new boolean [k * k]; int count = 1, empty = a [0] + B [0]-1; Queue <Data> queue = new Queue list <Data> (); int result [] = new int [k]; class Data {public int x, y, value; public Data (int x, int y) {this. x = x; this. y = y; this. value = a [x] + B [y] ;}} void main () {for (int I = 0; I <k * k; I ++) visited [I] = false; queue. add (new Data (0, 0); visited [0] = true; result [count-1] = a [0] + B [0]; while (! Queue. isEmpty () {Data data = queue. poll (); int t1 = empty, t2 = empty; // t1 t2 is easier to compare if (data. value! = Result [count-1]) {result [count] = data. value; if (++ count = k) break;} if (data. x + 1 <k &&(! Visited [(data. x + 1) * k + data. y]) {t1 = a [data. x + 1] + B [data. y]; visited [(data. x + 1) * k + data. y] = true;} if (data. y + 1 <k &&(! Visited [(data. x) * k + data. y + 1]) {t2 = a [data. x] + B [data. y + 1]; visited [(data. x) * k + data. y + 1] = true;} if (t1 <t2 & t1! = Empty) | (t1! = Empty & t2 = empty) | (t1 = t2 & t1! = Empty) {queue. add (new Data (data. x + 1, data. y);} if (t1> t2 & t2! = Empty) | (t2! = Empty & t1 = empty) | (t1 = t2 & t2! = Empty) {queue. add (new Data (data. x, data. y + 1) ;}}for (int I = 0; I <count; I ++) {System. out. print (result [I] + "") ;}} public static void main (String [] args) {new Test (). main ();}}
For more information, see this blog.
Http://blog.csdn.net/sunnianzhong/article/details/8932374
There are three methods, 1: brute force, 2: fast, 3: heap
My methods are not sorted, because they are ordered. I just cut unnecessary paths by queuing out of the queue according to the rules. There may be errors because there is not much data for verification. I can use a simple number as an example.
The algorithm has two sorted arrays, for example, a and B, which are merged into one array and return the median value.
I haven't written java code for a long time. The following code is equivalent to pseudocode. Let's briefly describe the algorithm!
Public int merge (int a [], int B []) {
Int lengtha = a. length, lengthb = B. length;
Int c [] = new array [lengtha + lengthb];
Int x = lengtha + lengthb, y = 0;
I = 0;
J = 0;
While (y <x ){
If (I <lengtha & j <lengthb ){
If (a [I] <= B [j]) {
C [y ++] = a [I ++];
}
Else {
C [y ++] = B [j ++];
}
}
Else if (I <lengtha ){
C [y ++] = a [I ++];
}
Else {
C [y ++] = B [j ++];
}
}
Return c [y/2];
}
Find two algorithms with the sum of x in an ordered array. the time complexity is at most nlgn.
I only want to give the algorithm IDEA enough: 1. First Use fast sorting (complexity nlogn) 2, and use an array's header pointer and tail pointer to move backward and forward respectively, and determine whether the sum of the two pointers is = x. If yes, the system stops. Otherwise, the system will continue to move and know that the two pointers meet each other. (Complexity n)