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