PAT A 1029 Median 双指针/二分

Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.

Given two increasing sequences of integers, you are asked to find their median.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤2×10​5​​) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of long int.

Output Specification:

For each test case you should output the median of the two given sequences in a line.

Sample Input:

4 11 12 13 14
5 9 10 15 16 17

Sample Output:

13

对于给定的两个有序序列,其长度分别为N1,N2,则合并后序列的中位数序号为(N1+N2-1)/2(下标从0开始)。然后让i、j分别指向两个序列首元素,cnt记录已经获取到新序列的个数,不断将两个序列中较小的数并入新序列中,对应指针向前移动,直到cnt大小等于中位数时,取两序列当前元素的较小值即为中位数。在算法开始之前可以将两序列末尾添加Long型最大值以解决访问越界问题。时间复杂度O(N1+N2)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

		// 第一个序列
		String[] line = reader.readLine().split(" ");
		int N1 = Integer.valueOf(line[0]);
		long[] num1 = new long[N1 + 1];
		for (int i = 0; i < N1; i++)
			num1[i] = Long.valueOf(line[i + 1]);
		// 第二个序列
		line = reader.readLine().split(" ");
		int N2 = Integer.valueOf(line[0]);
		long[] num2 = new long[N2 + 1];
		for (int i = 0; i < N2; i++)
			num2[i] = Long.valueOf(line[i + 1]);
		// 序列最后一个元素设为上限
		num1[N1] = num2[N2] = Long.MAX_VALUE;

		int cnt = 0, i = 0, j = 0;
		while (cnt < (N1 + N2 - 1) / 2) {// 未达到中间位置
			if (num1[i] < num2[j]) {// 选择较小的num1[i]
				i++;
			} else {
				j++;
			}
			cnt++;
		}
		if (num1[i] < num2[j])// 输出两个序列当前位置较小的元素
			System.out.println(num1[i]);
		else
			System.out.println(num2[j]);
	}
}

leetcode 4. 寻找两个有序数组的中位数

二分法 时间复杂度log(min(N1,N2))

	static double findMedianSortedArrays(int[] A, int[] B) {
		int m = A.length;
		int n = B.length;
		if (m > n) {// 保证m<=n
			int[] temp = A;
			A = B;
			B = temp;
			int tmp = m;
			m = n;
			n = tmp;
		}
		int iMin = 0, iMax = m, halfLen = (m + n + 1) / 2;
		while (iMin <= iMax) {
			int i = (iMin + iMax) / 2;// 二分,第一个数组分割点
			int j = halfLen - i;// 第二个数组分割点
			if (i < iMax && B[j - 1] > A[i]) {// i太小
				iMin = i + 1;
			} else if (i > iMin && A[i - 1] > B[j]) {// i太大
				iMax = i - 1;
			} else {// 完美
				int maxLeft = 0;//两个序列左半区间的最大值
				if (i == 0) {//第一个序列左半区间没有元素
					maxLeft = B[j - 1];
				} else if (j == 0) {
					maxLeft = A[i - 1];
				} else {
					maxLeft = Math.max(A[i - 1], B[j - 1]);
				}
				if ((m + n) % 2 == 1) {//总数为奇数,直接返回中间的数
					return maxLeft;
				}

				int minRight = 0;//右半区间最小值
				if (i == m) {
					minRight = B[j];
				} else if (j == n) {
					minRight = A[i];
				} else {
					minRight = Math.min(B[j], A[i]);
				}

				return (maxLeft + minRight) / 2.0;//返回中间两个数的平均值
			}
		}
		return 0.0;
	}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章