Ali intern written test program test questions

Last night, I accidentally saw a classmate doing the program coding test in the Ali intern written test. The question is given as follows. Two arrays A and B are assumed to have four elements: A=[a1,a2,a3,a4] B =[b1,b2,b3,b4];
Then insert the elements in B into A, and you can insert any number in any position in the A array. That is, it is possible to:
C=[a1,b1,b2,b3,b4,a2,a3,a4]; or C=[b1,a1,b2,b3,a2,a3,b4,a4], etc.;
Then multiply the elements in the new array by two and perform the sum operation again, and output the combination with the largest sum. (The order of 1234 cannot be changed)
The time limit for the question is 30 minutes, and I feel that the difficulty of the question is indeed a bit big in the limited time.
First thought is brute force:
vector<int> MaxSum(vector<int> A,vector<int> B){
	int sum=0;
	int length=A.size();
	int i=0;
	while(i<length){
		queue<int> Aq,Bq;
		for(int j=0;i<length;i++)
			Aq.push(A[i]);
		for(int j=0;i<length;i++)
			Bq.push(B[i]);
		vector<int> C;
		for(int j=0;j<i;j++){
			int k=Aq.front();
			C.push_back(k);
			Aq.pop();
		}
		
	}
}

Then I can't write so much. . . I feel that the situation is too complicated and difficult to analyze, so I dove. I don’t know if it will be easier to use a binary tree, but it is a bit difficult to simply use an array. Recursion is so hard! ! ! ! It's so hard to plug in, I feel like my mind is too stupid to solve it.
Then I thought about it this afternoon, and suddenly there was a flash of inspiration. Is it possible to select the smallest number between the two numbers and insert it into a new linked list, and then evaluate the array, because each comparison is in two arrays. The smallest element, so the relative product should also be the smallest group. So this solution is dynamic programming? Or greedy? My algorithm foundation is not good, I just feel that I can solve it like this, and I don't know if it can solve the problem, so I attach my own program, and I hope someone can explain it! I don't know the answer to this question, so it's just for you to make a reference and a record of your own learning:
vector<int> MaxSum(vector<int> A,vector<int> B){
	int sum=0;
	int length=A.size();
	int i=0;
	queue<int> Aq,Bq;
	for(int j=0;i<length;i++)
		Aq.push(A[i]);
	for(int j=0;i<length;i++)
		Bq.push(B[i]);
	vector<int> C;
	while(i<2*length){
		if(Aq.front()<Bq.front())
			C.push_back(Aq.front());
		else
			C.push_back(Bq.front());		
	}
	for(int k=0;k<C.size();){
		sum+=C[k]*C[k+1];
		k+=2;
	}
	return C;
}

It's a bit difficult to dove a pigeon! 30 minutes can't solve it, so it seems that Ali is far away from me!
I just looked at it and it seems that there is still a problem. The two queues Aq and Bq are not controlled to pop up, so the output is definitely wrong, so I changed it again:
vector<int> MaxSum(vector<int> A,vector<int> B){
	int sum=0;
	int length=A.size();
	int i=0;
	queue<int> Aq,Bq;
	for(int j=0;i<length;i++)
		Aq.push(A[i]);
	for(int j=0;i<length;i++)
		Bq.push(B[i]);
	vector<int> C;
	while(i<2*length){
		if(Aq.empty()||Bq.empty()) //Add the condition for jumping out of the loop
			break;
		if(Aq.front()<Bq.front()){
			C.push_back(Aq.front());
			Aq.pop();
		}
		else{
			C.push_back(Bq.front());
			Aq.pop();
		}		
	}
	if(Aq.empty()){//Add the insertion method when the queue of the other party is 0;
		while(!Bq.empty()){
			C.push_back(Bq.front());
			Bq.pop();
		}
	}
	else{
		while(!Aq.empty()){
			C.push_back(Aq.front());
			Aq.pop();
		}
	}
	for(int k=0;k<C.size();){
		sum+=C[k]*C[k+1];
		k+=2;
	}
	return C;
}
Alright, alright, that's it in the end, it's not going to change, it's a headache to write.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325728742&siteId=291194637