思路:
桶排序
N个数,设置 N+ 1 个桶,,一定有一个空桶,,为的是保证最大差值一定是不是出现在同一个桶中;
只要比较 非空桶 的最小值,与前一个 非空桶的最大值,求 最大的差值,
1 package my_basic; 2 3 import java.text.Bidi; 4 import java.util.Arrays; 5 6 public class MaxGap { 7 8 /*给定一个数组,求如果排序之后,相邻两数的最大差值,要求时 间复杂度O(N),且要求不能用非基于比较的排序*/ 9 10 public static int maxGap(int[] arr) { 11 if (arr == null || arr.length < 2) { 12 return 0; 13 } 14 //获取最小值 最大值 15 int len = arr.length; 16 int min = Integer.MAX_VALUE; 17 int max = Integer.MIN_VALUE; 18 for (int i = 0; i < arr.length; i++) { 19 min = Math.min(min, arr[i]); 20 max = Math.max(max, arr[i]); 21 } 22 if (min == max) { 23 return 0; 24 } 25 //放到桶里 26 boolean[] hasNum = new boolean[len + 1]; 27 int[] mins = new int[len + 1]; 28 int[] maxs = new int[len + 1]; 29 30 int bid = 0; 31 // for (int i = 0; i < arr.length; i++) { 32 // bid = bucket(arr[i], len, min, max); 33 // hasNum[bid] = true; 34 // mins[bid] = Math.min(mins[bid], arr[i]); 35 // maxs[bid] = Math.max(maxs[bid], arr[i]); 36 // } 37 for (int i = 0; i < len; i++) { 38 bid = bucket(arr[i], len, min, max); 39 mins[bid] = hasNum[bid] ? Math.min(mins[bid], arr[i]) : arr[i]; 40 maxs[bid] = hasNum[bid] ? Math.max(maxs[bid], arr[i]) : arr[i]; 41 hasNum[bid] = true; 42 } 43 44 //找最大间距 45 int res =0; 46 int m = maxs[0]; 47 for(int i=1 ;i <= len;i++) { 48 if (hasNum[i]) { 49 res = (mins[i] - m)>res ? (mins[i] - m) : res; 50 m = maxs[i]; 51 } 52 } 53 return res; 54 55 } 56 57 //放到几号桶里 58 public static int bucket(long num, long len, long min, long max) { 59 return (int) ((num - min) * len / (max - min)); 60 } 61 62 //对数器 63 public static int[] generateRandomArray(int maxSize, int maxValue) { 64 int[] arr = new int[(int) ((maxSize + 1) * Math.random())]; 65 for (int i = 0; i < arr.length; i++) { 66 arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random()); 67 } 68 return arr; 69 } 70 public static int comparetor(int[] arr) { 71 if (arr==null || arr.length < 2) { 72 return 0; 73 } 74 Arrays.sort(arr); 75 // printArray(arr); 76 int res=0; 77 for (int i = 1; i < arr.length; i++) { 78 res = (arr[i] - arr[i-1]) > res? (arr[i] - arr[i-1]) :res; 79 } 80 return res; 81 } 82 83 public static void printArray(int[] arr) { 84 for (int i = 0; i < arr.length; i++) { 85 System.out.print(arr[i]+" "); 86 } 87 System.out.println(); 88 } 89 public static int[] copyArray(int[] arr) { 90 if (arr == null) { 91 return null; 92 } 93 int[] res = new int[arr.length]; 94 for (int i = 0; i < arr.length; i++) { 95 res[i] = arr[i]; 96 } 97 return res; 98 } 99 100 public static void main(String[] args) { 101 int maxSize =10; 102 int maxValue = 50; 103 boolean succeed = true; 104 int[] arr1 = generateRandomArray(maxSize, maxValue); 105 printArray(arr1); 106 int res = maxGap(arr1); 107 108 int[] arr2 = copyArray(arr1); 109 int res2 = comparetor(arr2); 110 if (res != res2) { 111 printArray(arr1); 112 printArray(arr2); 113 System.out.println(res); 114 System.out.println(res2); 115 116 }else { 117 System.out.println("nice!"); 118 } 119 120 } 121 122 123 124 }