經典算法(桶排序實現):一個無序數組,排序後返回相鄰兩數的最大差值

題目:

給定一個long類型無序數組,排序後求出相鄰兩個數的最大差值,並返回。(要求 時間複雜度爲O(n))

解析:題中給出的是long類型的數組,因此數字有可能非常大,排除基數排序方法,容易看出這道題目不能用一般的排序方法。實現方法如下:
		首先求出數組中元素的個數n、最小值min、最大值max;根據數據的個數建立桶:
	設置桶的個數爲n+1,將min~max範圍的數平分爲n+1等分依次作爲這n+1個桶可放入
	數的值範圍。
		例如:數組Arr[9]={0,45,47,27,22,31,83,23,99},其中n=9,min=0,max=99;因此
	設置10個桶如下圖:

例圖:

具體操作如下:
	遍歷該數組,依次將數組中元素按照範圍劃分放入對應的桶中,顯然桶1和桶10中必然分別存放有min和max,
由於有9個數,而桶個數爲10個,所以在桶2~桶9之間至少存在一個空桶。可以知道桶內的最大差值最大情況爲9,
空桶兩邊相鄰非空桶之間最大差值最小情況爲11。因此我們可以得到一個結論:相鄰兩個數的最大差值一定存在
於桶與桶之間,而不可能在同一個桶內。所以就不用關心一個桶內部的信息,只關心一個桶內的最大數和最小數
即可。且相鄰非空桶之間後者的最小值與前者的最大值肯定是排完序後相鄰的兩個數。
	給定三個數組:Boolean isEmpty[10],int Min[10],int Max[10],這三個數組每個位置信息作爲每個桶
的狀態(是否爲空,最小值,最大值)

例圖

	相鄰非空桶之間後面一個桶的最小值減去前面一個桶的最大值爲候選答案之一。遍歷一遍該10個桶則可得到所求
答案。時間複雜度爲:O(n),此題分析完畢。

代碼如下:(非自己編寫,懶的寫)

package basic_class_01;

import java.util.Arrays;

public class Code_11_MaxGap {

	public static int maxGap(int[] nums) {
		if (nums == null || nums.length < 2) {
			return 0;
		}
		int len = nums.length;
		int min = Integer.MAX_VALUE;
		int max = Integer.MIN_VALUE;
		for (int i = 0; i < len; i++) {
			min = Math.min(min, nums[i]);
			max = Math.max(max, nums[i]);
		}
		if (min == max) {
			return 0;
		}
		boolean[] hasNum = new boolean[len + 1];
		int[] maxs = new int[len + 1];
		int[] mins = new int[len + 1];
		int bid = 0;
		for (int i = 0; i < len; i++) {
			bid = bucket(nums[i], len, min, max);
			mins[bid] = hasNum[bid] ? Math.min(mins[bid], nums[i]) : nums[i];
			maxs[bid] = hasNum[bid] ? Math.max(maxs[bid], nums[i]) : nums[i];
			hasNum[bid] = true;
		}
		int res = 0;
		int lastMax = maxs[0];
		int i = 1;
		for (; i <= len; i++) {
			if (hasNum[i]) {
				res = Math.max(res, mins[i] - lastMax);
				lastMax = maxs[i];
			}
		}
		return res;
	}

	public static int bucket(long num, long len, long min, long max) {
		return (int) ((num - min) * len / (max - min));
	}

	// for test
	public static int comparator(int[] nums) {
		if (nums == null || nums.length < 2) {
			return 0;
		}
		Arrays.sort(nums);
		int gap = Integer.MIN_VALUE;
		for (int i = 1; i < nums.length; i++) {
			gap = Math.max(nums[i] - nums[i - 1], gap);
		}
		return gap;
	}

	// for test
	public static int[] generateRandomArray(int maxSize, int maxValue) {
		int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
		}
		return arr;
	}

	// for test
	public static int[] copyArray(int[] arr) {
		if (arr == null) {
			return null;
		}
		int[] res = new int[arr.length];
		for (int i = 0; i < arr.length; i++) {
			res[i] = arr[i];
		}
		return res;
	}

	// for test
	public static void main(String[] args) {
		int testTime = 500000;
		int maxSize = 100;
		int maxValue = 100;
		boolean succeed = true;
		for (int i = 0; i < testTime; i++) {
			int[] arr1 = generateRandomArray(maxSize, maxValue);
			int[] arr2 = copyArray(arr1);
			if (maxGap(arr1) != comparator(arr2)) {
				succeed = false;
				break;
			}
		}
		System.out.println(succeed ? "Nice!" : "Fucking fucked!");
	}

}

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