Given an unsorted array, find the maximum difference between the successive elements in its sorted form.
Try to solve it in linear time/space.
Return 0 if the array contains less than 2 elements.
You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
1. Bucket Sort
what we use here firstly is Bucket Sort
Can refer to my another blog!
Sort_Algorithm
Java Solution
public class Solution {
public int maximumGap(int[] nums) {
if (nums == null || nums.length < 2)
return 0;
int min = nums[0];
int max = nums[0];
for (int i : nums) {
min = Math.min(min, i);
max = Math.max(max, i);
}
// ceil(12.2) = 13 http://www.tutorialspoint.com/java/lang/math_ceil.htm
int gap = (int)Math.ceil((double)(max - min)/(nums.length - 1));
int[] bucketsMin = new int[nums.length - 1]; //store the min value in certain index bucket
int[] bucketsMax = new int[nums.length - 1]; //store the max value in certain index bucket
Arrays.fill(bucketsMin, Integer.MAX_VALUE);
Arrays.fill(bucketsMax, Integer.MIN_VALUE);
for (int i : nums) {
if (i == min || i == max) continue;
int index = (i - min) / gap;
bucketsMin[index] = Math.min(i, bucketsMin[index]);
bucketsMax[index] = Math.max(i, bucketsMax[index]);
}
int maxGap = Integer.MIN_VALUE;
int privious = min;
for (int j = 0; j < nums.length - 1; j++) {
if (bucketsMin[j] == Integer.MAX_VALUE && bucketsMax[j] == Integer.MIN_VALUE) continue;
// substract the privious max value from next buckets' min value, iterative!
maxGap = Math.max(maxGap, bucketsMin[j] - privious);
privious = bucketsMax[j];
}
maxGap = Math.max(maxGap, max - privious);
return maxGap;
}
}
Python Solution
class Solution(object):
def maximumGap(self, nums):
if len(nums)<2:
return 0
maxV, minV = max(nums), min(nums)
maxGap = (maxV-minV)//(len(nums)-1)
bSize = maxGap+1
buckets = [[] for _ in range((maxV-minV)//bSize+1)]
for n in nums:
buckets[(n-minV)//bSize].append(n)
buckets = [b for b in buckets if b]
for i in range(1, len(buckets)):
maxGap = max(maxGap, min(buckets[i])-max(buckets[i-1]))
return maxGap
2. Radix Sort
public class Solution {
public int maximumGap(int[] nums) {
if (nums == null || nums.length < 2) {
return 0;
}
// m is the maximal number in nums
int m = nums[0];
for (int i = 1; i < nums.length; i++) {
m = Math.max(m, nums[i]);
}
int exp = 1; // 1, 10, 100, 1000 ...
int R = 10; // 10 digits
int[] aux = new int[nums.length];
while (m / exp > 0) { // Go through all digits from LSB to MSB
int[] count = new int[R];
for (int i = 0; i < nums.length; i++) {
count[(nums[i] / exp) % 10]++;
}
for (int i = 1; i < count.length; i++) {
count[i] += count[i - 1];
}
for (int i = nums.length - 1; i >= 0; i--) {
aux[--count[(nums[i] / exp) % 10]] = nums[i];
}
for (int i = 0; i < nums.length; i++) {
nums[i] = aux[i];
}
exp *= 10;
}
int max = 0;
for (int i = 1; i < aux.length; i++) {
max = Math.max(max, aux[i] - aux[i - 1]);
}
return max;
}
}
- The first step is to find the maximum value in nums array, it will
be the threshold to end while loop. - Then use the radix sort algorithm to sort based on each digit from Least Significant Bit (LSB) to Most Significant Bit (MSB), that’s exactly what’s showing in the link.
(nums[i] / exp) % 10
is used to get the digit, for each digit, basically the digit itself serves as the index to access the count array. Count array stores the index to access aux array which stores the numbers after sorting based on the current digit.- Finally, find the maximum gap from sorted array.
Time and space complexities are both O(n)
. (Actually time is O(10n) at worst case for Integer.MAX_VALUE 2147483647)