Java for LeetCode 163 Maximum Gap

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.

Credits:
Special thanks to @porker2008 for adding this problem and creating all test cases.

解题思路:

由于要用到线性时间复杂度,比较排序已经不适用了(《算法导论》P108),能够用到的有 计数排序、基数排序、堆排序,由于计数排序比较适合小范围的,基数排序最终会将顺序排好,而我们不需要那么复杂,因此,可以用桶排序,适当选择桶之间的距离,保证最大的successive distance在两桶之间即可,JAVA实现如下:

    public int maximumGap(int[] nums) {
		if (nums.length <= 1)
			return 0;
		int min = nums[0], max = nums[0];
		for (int num : nums) {
			min = Math.min(min, num);
			max = Math.max(max, num);
		}
		if (max == min)
			return 0;
		int distance = Math.max(1, (max - min) / (nums.length - 1));
		int bucket[][] = new int[(max - min) / distance + 1][2];
		for (int i = 0; i < bucket.length; i++) {
			bucket[i][0] = Integer.MAX_VALUE;
			bucket[i][1] = -1;
		}
		for (int num : nums) {
			int i = (num - min) / distance;
			bucket[i][0] = Math.min(num, bucket[i][0]);
			bucket[i][1] = Math.max(num, bucket[i][1]);
		}
		int maxDistance = 1, left = -1, right = -1;
		for (int i = 0; i < bucket.length; i++) {
			if (bucket[i][1] == -1)
				continue;
			if (right == -1) {
				right = bucket[i][1];
				continue;
			}
			left = bucket[i][0];
			maxDistance=Math.max(maxDistance, left-right);
			right=bucket[i][1];
		}
		return maxDistance;
    }
时间: 2024-08-05 15:01:35

Java for LeetCode 163 Maximum Gap的相关文章

LeetCode – Refresh – Maximum Gap

Sorting solution O(nlogn): 1 class Solution { 2 public: 3 int maximumGap(vector<int> &num) { 4 int len = num.size(), result = 0; 5 if (len < 2) return 0; 6 sort(num.begin(), num.end()); 7 for (int i = 0; i < len-1; i++){ 8 result = max(res

leetcode 155: Maximum Gap

Maximum Gap Total Accepted: 2946 Total Submissions: 12695 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 elemen

LeetCode[Sort]: Maximum Gap

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-negat

Java for LeetCode 104 Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 解题思路: 递归,JAVA实现如下: public int maxDepth(TreeNode root) { if(root==null) return 0; return Ma

【leetcode】Maximum Gap(hard)★

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-negat

【leetcode】Maximum Gap

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-negat

Java for LeetCode 053 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. 解题思路: 看题目,一次遍历肯定能搞定问题,只需使用一个sum

Java for LeetCode 152 Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 解题思路: 计算连续的积最大,由于会有负数出现,因此需要用两个int表示包含num

leetcode 164:Maximum Gap

题意: 给定非排序数组,找出其排序后相邻元素的最大差值. 线性时间空间.元素数少于2时返回0.元素值非负且int范围内. 思路: 排序最快nlogn不符合要求: 参考网上,学习了桶排序的方法: 桶排序:按值分段处理: 设定桶大小和桶个数: 因为ans>=(MAX-MIN)/(len-1): 桶大小:(MAX-MIN)/(len-1)向上取整,(注意为0时取1) 桶个数:(MAX-MIN)/桶大小+1: ans取值只可能是桶A的最小值-桶B的最大值形式:不会在桶内取:(因为桶内最大差值小于桶大小)