[LeetCode] Maximum Gap

简介: This problem has a naive solution using sort and linear scan. The suggested solution uses the idea of bucket sort.

This problem has a naive solution using sort and linear scan. The suggested solution uses the idea of bucket sort. The following is a C++ implementation of the suggested solution.

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 - u) / 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.

Putting these together, we can have the following solution, barely a straight-forward implementation of the suggested solution.

 1 class Solution {
 2 public:
 3     int maximumGap(vector<int>& nums) {
 4         int n = nums.size();
 5         if (n < 2) return 0;
 6         auto lu = minmax_element(nums.begin(), nums.end());
 7         int l = *lu.first, u = *lu.second;
 8         int gap = max((u - l) / (n - 1), 1);
 9         int m = (u - l) / gap + 1;
10         vector<vector<int>> buckets(m, {INT_MAX, INT_MIN});
11         for (int num : nums) {
12             int k = (num - l) / gap;
13             if (num < buckets[k][0]) buckets[k][0] = num;
14             if (num > buckets[k][1]) buckets[k][1] = num;
15         }
16         int i = 0, j;
17         gap = buckets[0][1] - buckets[0][0];
18         while (i < m) {
19             j = i + 1;
20             while (j < m && buckets[j][0] == INT_MAX && buckets[j][1] == INT_MIN)
21                 j++;
22             if (j == m) break;
23             gap = max(gap, buckets[j][0] - buckets[i][1]);
24             i = j;
25         }
26         return gap; 
27     }
28 };

 

目录
相关文章
Leetcode 53.Maximum Subarray
题意简单,给出一个数组,求出其中最大的子数组和。 这种简单题目背后蕴藏着很巧妙的解题方法。其实只需要遍历一次数组就可以求得解。 思路是这样的,你想想看,如果一段子数组的和是负数, 那么这一段子数组不可能是最大和数组的一部分,丢掉重新从下一个位置开始选。
50 0
|
算法
LeetCode 414. Third Maximum Number
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
97 0
LeetCode 414. Third Maximum Number
LeetCode 318. Maximum Product of Word Lengths
给定一个字符串数组 words,找到 length(word[i]) * length(word[j]) 的最大值,并且这两个单词不含有公共字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词,返回 0。
81 0
LeetCode 318. Maximum Product of Word Lengths
LeetCode 239. Sliding Window Maximum
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口 k 内的数字。滑动窗口每次只向右移动一位。 返回滑动窗口最大值。
64 0
LeetCode 239. Sliding Window Maximum
LeetCode 164. Maximum Gap
给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值。 如果数组元素个数小于 2,则返回 0。
84 0
LeetCode 164. Maximum Gap
LeetCode 152. Maximum Product Subarray
给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。
51 0
LeetCode 152. Maximum Product Subarray
LeetCode 104. Maximum Depth of Binary Tree
给定一颗二叉树,返回其最大深度. 注意:深度从1开始计数.
67 0
LeetCode 104. Maximum Depth of Binary Tree
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode Contest 186 5392. 分割字符串的最大得分 Maximum Score After Splitting a String
LeetCode Contest 186 5392. 分割字符串的最大得分 Maximum Score After Splitting a String
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree