[LeetCode]Maximum Gapd
来源:互联网 发布:网络律师 编辑:程序博客网 时间:2024/05/01 16:27
int bucket_size = Math.max(1,(Max-Min)/(N) + 1);//ceil( (Max-Min)/(N-1) ) = (Max-Min-1)/(N-1) + 1or int bucket_size = (int)Math.ceil( ((double)(Max-Min))/(N-1) );int bucket_count = (Max-Min)/bucket_size + 1;
下面用桶排序实现,这也是leetcode上给出的参考解法:
Suppose there are N elements and they range from A to B.
Then the maximum gap will be no smaller than ceiling[(B - A) / (N - 1)]
Let the length of a bucket to be len = ceiling[(B - A) / (N - 1)], then we will have at most num = (B - A) / len + 1 of bucket
for any number K in the array, we can easily find out which bucket it belongs by calculating loc = (K - A) / len and therefore maintain the maximum and minimum elements in each bucket.
Since the maximum difference between elements in the same buckets will be at most len - 1, so the final answer will not be taken from two elements in the same buckets.
For each non-empty buckets p, find the next non-empty buckets q, then q.min - p.max could be the potential answer to the question. Return the maximum of all those values.
- [LeetCode]Maximum Gapd
- LeetCode: Maximum Subarray
- LeetCode Maximum Subarray
- [Leetcode] Maximum Subarray
- LeetCode: Maximum Subarray
- leetcode 25: Maximum Subarray
- [LeetCode] Maximum Subarray
- [Leetcode] Maximum Subarray
- [Leetcode] Maximum Subarrary
- leetcode Sliding Window Maximum
- [LeetCode]Maximum Subarray
- [leetcode]Maximum Subarray
- LeetCode-Maximum Subarray
- [leetcode] Maximum Subarray
- LeetCode 45: Maximum Subarray
- LeetCode - Maximum Subarray
- LeetCode:Maximum Subarray
- LeetCode 53: Maximum Subarray
- 神奇的与(&)、非(~)、或(|)、异或(^)
- 最短路_POJ_3268
- CSS3 圆转动 特效
- BootStrap Panel
- iOS应用架构谈 开篇
- [LeetCode]Maximum Gapd
- Chang the default encoding of notepad
- 多线程-锁的类型 && CAS 非阻塞同步
- Fragment中menu菜单注意事项
- 利用UIScrollView实现图片的缩放
- [UVA1619]Feel Good[DP]
- 使用 Spring Security 保护 Web 应用的安全
- 『Others』【English】The road to enlarge my vocabulary
- [IOS]今天开始学UI---UIAlertController