ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 0164 Maximum Gap

2022-05-27 21:03:39  阅读:180  来源: 互联网

标签:nums int max Maximum Gap num gap LeetCode maximum


原题传送门

1. 题目描述

2. Solution 1

1、思路分析
You can look at radix sort visualization here before reading the code:
https://www.cs.usfca.edu/~galles/visualization/RadixSort.html

1> The first step is to find the maximum value in nums array, it will be the threshold to end while loop.
2> 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.
3> (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.
4> Finally, find the maximum gap from sorted array.
Time and space complexities are both O(n).

2、代码实现

package Q0199.Q0164MaximumGap;

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 num : nums) count[(num / 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];

            System.arraycopy(aux, 0, nums, 0, nums.length);

            exp *= 10;
        }

        int max = 0;
        for (int i = 1; i < aux.length; i++) max = Math.max(max, aux[i] - aux[i - 1]);

        return max;
    }
}

3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(n)

3. Solution 2

1、思路分析
设长度为N的数组中最大值为max,min,则不难发现相邻数字的最大间距不会小于(max - min) / (N - 1)。反证法: 设相邻数字的最大间距都小于(max - min) / (N - 1),并记排序后从小到大的数字依次为A[1], A[2], ..., A[N],则有:
A[N] - A[1] = (AN - A[N-1])+ ... + (A[2] - A[1])
< (max - min) / (N - 1) + (max - min) / (N - 1) + ... + (max - min) / (N - 1)
= max - min
但根据A[1], A[N]的定义,一定有A[1]=min, A[N]=max,矛盾。

Suppose all the n elements in nums fall within [l, u], the maximum gap will not be smaller than gap = (u - l) / (n - 1). However, this gap may become 0 and so we take the maximum of it with 1 to guarantee that the gap used to create the buckets is meaningful.

Then there will be at most m = (u - l) / gap + 1 buckets. For each number num, it will fall in the k = (num - l) / gap bucket. After putting all elements of nums in the corresponding buckets, we can just scan the buckets to compute the maximum gap.

The maximum gap is only dependent on the maximum number of the current bucket and the minimum number of the next neighboring bucket (the bucket should not be empty). So we only store the minimum and the maximum of each bucket. Each bucket is initialized as {minimum = INT_MAX, maximum = INT_MIN} and then updated while updating the buckets.

2、代码实现

package Q0199.Q0164MaximumGap;

import java.util.Arrays;

public class Solution2 {
    public int maximumGap(int[] nums) {
        int n = nums.length;
        if (n < 2) return 0;
        int l = Arrays.stream(nums).min().getAsInt();
        int u = Arrays.stream(nums).max().getAsInt();
        int gap = Math.max((u - l) / (n - 1), 1);
        int m = (u - l) / gap + 1;
        int[] bucketsMin = new int[m], bucketsMax = new int[m];
        Arrays.fill(bucketsMin, Integer.MAX_VALUE);
        Arrays.fill(bucketsMax, Integer.MIN_VALUE);
        for (int num : nums) {
            int k = (num - l) / gap;
            if (num < bucketsMin[k]) bucketsMin[k] = num;
            if (num > bucketsMax[k]) bucketsMax[k] = num;
        }
        int i = 0, j;
        gap = bucketsMax[0] - bucketsMin[0];
        while (i < m) {
            j = i + 1;
            while (j < m && bucketsMin[j] == Integer.MAX_VALUE && bucketsMax[j] == Integer.MIN_VALUE) j++;
            if (j == m) break;
            gap = Math.max(gap, bucketsMin[j] - bucketsMax[i]);
            i = j;
        }
        return gap;
    }
}

3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(n)

标签:nums,int,max,Maximum,Gap,num,gap,LeetCode,maximum
来源: https://www.cnblogs.com/junstat/p/16319077.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有