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;
	}

 

 

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