|
4 | 4 |
|
5 | 5 | /**
|
6 | 6 | * Given an unsorted array, find the maximum difference between the successive elements in its sorted form.
|
7 |
| -
|
8 |
| - Try to solve it in linear time/space. |
9 |
| -
|
10 |
| - Return 0 if the array contains less than 2 elements. |
11 |
| -
|
12 |
| - You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range. |
| 7 | + * <p> |
| 8 | + * Try to solve it in linear time/space. |
| 9 | + * <p> |
| 10 | + * Return 0 if the array contains less than 2 elements. |
| 11 | + * <p> |
| 12 | + * You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range. |
13 | 13 | */
|
14 | 14 | public class MaximumGap {
|
| 15 | + //brute force |
15 | 16 | public int maximumGap(int[] nums) {
|
16 |
| - if(nums.length < 2) return 0; |
| 17 | + if (nums.length < 2) return 0; |
17 | 18 |
|
18 | 19 | Arrays.sort(nums);
|
19 | 20 | int max = Integer.MIN_VALUE;
|
20 |
| - for(int i = 1; i < nums.length;){ |
21 |
| - while(i < nums.length && nums[i] == nums[i-1]){ |
| 21 | + for (int i = 1; i < nums.length; ) { |
| 22 | + while (i < nums.length && nums[i] == nums[i - 1]) { |
22 | 23 | i++;
|
23 | 24 | }
|
24 |
| - if(i == nums.length) { |
| 25 | + if (i == nums.length) { |
25 | 26 | i--;
|
26 |
| - max = (nums[i] - nums[i-1] > max) ? nums[i] - nums[i-1] : max; |
| 27 | + max = (nums[i] - nums[i - 1] > max) ? nums[i] - nums[i - 1] : max; |
27 | 28 | break;
|
28 |
| - } |
29 |
| - else max = (nums[i] - nums[i-1] > max) ? nums[i] - nums[i-1] : max; |
30 |
| - if(nums[i] != nums[i-1]){ |
| 29 | + } else max = (nums[i] - nums[i - 1] > max) ? nums[i] - nums[i - 1] : max; |
| 30 | + if (nums[i] != nums[i - 1]) { |
31 | 31 | i++;
|
32 | 32 | }
|
33 | 33 | }
|
34 | 34 | return max;
|
35 | 35 | }
|
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + //http://www.programcreek.com/2014/03/leetcode-maximum-gap-java/ |
| 40 | + class Bucket { |
| 41 | + int min = -1; |
| 42 | + int max = -1; |
| 43 | + |
| 44 | + public Bucket() { |
| 45 | + this.min = -1; |
| 46 | + this.max = -1; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + //compute interval and multiply by interval to get the index |
| 51 | + public int maximumGap_from_programcreek_1(int[] nums) { |
| 52 | + if (nums == null || nums.length < 2) return 0; |
| 53 | + |
| 54 | + int maxNum = nums[0]; |
| 55 | + int minNum = nums[0]; |
| 56 | + for (int i = 0; i < nums.length; i++) { |
| 57 | + maxNum = Math.max(maxNum, nums[i]); |
| 58 | + minNum = Math.min(minNum, nums[i]); |
| 59 | + } |
| 60 | + |
| 61 | + //initialize bucket array |
| 62 | + Bucket[] buckets = new Bucket[nums.length + 1]; |
| 63 | + for (int i = 0; i < buckets.length; i++) { |
| 64 | + buckets[i] = new Bucket(); |
| 65 | + } |
| 66 | + |
| 67 | + double interval = (double) nums.length/(maxNum - minNum); |
| 68 | + //distribute the array to different buckets |
| 69 | + for (int i = 0; i < nums.length; i++) { |
| 70 | + int index = (int) ((nums[i] - minNum) * interval); |
| 71 | + if (buckets[index].min == -1) { |
| 72 | + buckets[index].min = nums[i]; |
| 73 | + buckets[index].max = nums[i]; |
| 74 | + } else { |
| 75 | + buckets[index].min = Math.min(nums[i], buckets[index].min); |
| 76 | + buckets[index].max = Math.max(nums[i], buckets[index].max); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + //scan through the bucket array to find the maximal gap |
| 81 | + int result = 0; |
| 82 | + int prev = buckets[0].max; |
| 83 | + for (int i = 1; i < buckets.length; i++) { |
| 84 | + if (buckets[i].min != -1) { |
| 85 | + result = Math.max(result, buckets[i].min - prev); |
| 86 | + prev = buckets[i].max; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return result; |
| 91 | + } |
| 92 | + |
| 93 | + //compute gap and divide by gap to get the index |
| 94 | + public int maximumGap_from_programcreek_2(int[] nums) { |
| 95 | + if (nums == null || nums.length < 2) return 0; |
| 96 | + |
| 97 | + int maxNum = nums[0]; |
| 98 | + int minNum = nums[0]; |
| 99 | + for (int i = 0; i < nums.length; i++) { |
| 100 | + maxNum = Math.max(maxNum, nums[i]); |
| 101 | + minNum = Math.min(minNum, nums[i]); |
| 102 | + } |
| 103 | + |
| 104 | + //initialize bucket array |
| 105 | + Bucket[] buckets = new Bucket[nums.length + 1]; |
| 106 | + for (int i = 0; i < buckets.length; i++) { |
| 107 | + buckets[i] = new Bucket(); |
| 108 | + } |
| 109 | + |
| 110 | + double gap = (double) (maxNum - minNum)/(nums.length-1); |
| 111 | + //distribute the array to different buckets |
| 112 | + for (int i = 0; i < nums.length; i++) { |
| 113 | + int index = (int) ((nums[i] - minNum)/gap); |
| 114 | + if (buckets[index].min == -1) { |
| 115 | + buckets[index].min = nums[i]; |
| 116 | + buckets[index].max = nums[i]; |
| 117 | + } else { |
| 118 | + buckets[index].min = Math.min(nums[i], buckets[index].min); |
| 119 | + buckets[index].max = Math.max(nums[i], buckets[index].max); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + //scan through the bucket array to find the maximal gap |
| 124 | + int result = 0; |
| 125 | + int prev = buckets[0].max; |
| 126 | + for (int i = 1; i < buckets.length; i++) { |
| 127 | + if (buckets[i].min != -1) { |
| 128 | + result = Math.max(result, buckets[i].min - prev); |
| 129 | + prev = buckets[i].max; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return result; |
| 134 | + } |
36 | 135 | }
|
0 commit comments