leetcode-4. Finding the median of two positive arrays

Posted by kooper on Thu, 14 May 2020 16:09:57 +0200

leetcode-4. Find the median of two positive arrays.

Given two positive (small to large) arrays of sizes m and n, nums1 and nums2.

Please find the median of these two positive sequence arrays, and the time complexity of the algorithm is required to be O(log(m + n)).

You can assume that nums1 and nums2 are not both empty.

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

Then the median is (2 + 3) / 2 = 2.5

Source: LeetCode
Link: https://leetcode-cn.com/problems/two-sum
Copyright belongs to the network. For commercial reprint, please contact the official authorization. For non-commercial reprint, please indicate the source.



Solution 1: auxiliary array merges the original two arrays.
double findMedianSortedArrays( int *nums1, int nums1Size, int *nums2, int nums2Size ) {
	double answer = 0.0;
	int i1 = 0, i2 = 0, ti = 0, *tempArray = NULL;

	tempArray = malloc( sizeof(*tempArray) * (nums1Size + nums2Size) );

	while( i1 < nums1Size && i2 < nums2Size ) {
		tempArray[ti++] = nums1[i1] <= nums2[i2] ? nums1[i1++] : nums2[i2++];
	}
	while( i1 < nums1Size ) {
		tempArray[ti++] = nums1[i1++];
	}
	while( i2 < nums2Size ) {
		tempArray[ti++] = nums2[i2++];
	}

	answer = (ti & 1) ? tempArray[ti / 2] * 2 : tempArray[ti / 2] + tempArray[ti / 2 - 1];

	free( tempArray );

	return answer / 2.0;
}


Solution 2: simulate the merging process. In fact, do not need to merge the two arrays, just find the median in the position
double findMedianSortedArrays( int *nums1, int nums1Size, int *nums2, int nums2Size ) {
	int lasttime = 0, current = 0, len = nums1Size + nums2Size;
	int half = len / 2;

	for( int i1 = 0, i2 = 0, i = 0; i <= half; ++i ) {
		lasttime = current; // Record the value of the last position in the median
		if( i2 >= nums2Size || (i1 < nums1Size && nums1[i1] <= nums2[i2]) ) {
			current = nums1[i1++];
		} else if( i2 < nums2Size ) {
			current = nums2[i2++];
		}
	}

	return len & 1 ? current : (lasttime + current) / 2.0;
}


Solution 3: finding the median is actually a special case of finding the k-th decimal. In the traversal process of the previous solution, only one element that cannot be the median is removed at a time,
Because the sequence is ordered, in fact, it can be eliminated by half,
Suppose you want to find the k-th small element, and exclude k/2 elements every time
reference resources: https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-w-2/
#define MIN( a, b ) ((a) <= (b) ? (a) : (b))

static int recursion( int a[], int aSize, int b[], int bSize, int k ) {
	int kc2 = k / 2;
	int ai = MIN( aSize, kc2 ) - 1, bi = MIN( bSize, kc2 ) - 1;

	if( aSize < 1 || bSize < 1 ) {
		return aSize < 1 ? b[k - 1] : a[k - 1];
	}
	if( k < 2 ) {
		return MIN( a[0], b[0] );
	}
	if( a[ai] <= b[bi] ) {
		return recursion( a + MIN( aSize, ai + 1 ), aSize - (ai + 1), b, bSize, k - (ai + 1) );
	}

	return recursion( a, aSize, b + MIN( bSize, bi + 1 ), bSize - (bi + 1), k - (bi + 1) );
}

double findMedianSortedArrays( int *nums1, int nums1Size, int *nums2, int nums2Size ) {
	int len = nums1Size + nums2Size;
	double answer = 0.0;

	// Suppose the array has 7 elements, then:
	// Median = (4th + 4th) / 2.0
	// Median = (8th / 2nd + 9th / 2nd) / 2.0

	// If the array has 8 elements in total, then:
	// Median = (4th + 5th) / 2.0
	// Median = (9th / 2nd + 10th / 2nd) / 2.0

	// The number of elements in an array, whether odd or even,
	// The median is (the ((len+1)/2) element + the ((len+2)/2) element) / 2.0

	answer += recursion( nums1, nums1Size, nums2, nums2Size, (len + 1) / 2 );
	answer += recursion( nums1, nums1Size, nums2, nums2Size, (len + 2) / 2 );

	return answer / 2.0;
}



Topics: C network