leetcode:sort:Maximum Gap(164)
来源:互联网 发布:下载宇龙汽修仿真软件 编辑:程序博客网 时间:2024/06/05 00:16
https://leetcode.com/problems/maximum-gap/
http://www.cnblogs.com/ganganloveu/p/4162290.html
class Solution {public: int maximumGap(vector<int>& nums) { if(nums.empty() || nums.size() == 1) return 0; int n = nums.size(); int minAll = *min_element(nums.begin(), nums.end()); int maxAll = *max_element(nums.begin(), nums.end()); // type conversion!!! double gap = ((double)(maxAll - minAll)) / (n - 1); // compute min and max element for each bucket vector<int> minV(n-1, INT_MAX); vector<int> maxV(n-1, INT_MIN); for(int i = 0; i < n; i ++) { if(nums[i] != maxAll) {// the bktId of maxAll will fall out of bucket range int bktId = (int)((nums[i]-minAll)/gap); minV[bktId] = min(minV[bktId], nums[i]); maxV[bktId] = max(maxV[bktId], nums[i]); } } int ret = 0; int curMax = maxV[0]; for(int i = 1; i < n-1; i ++) { if(minV[i] != INT_MAX) { ret = max(ret, minV[i]-curMax); curMax = maxV[i]; } } ret = max(ret, maxAll-curMax); return ret; }};
0 0
- leetcode:sort:Maximum Gap(164)
- leetcode:sort:Maximum Gap(164)
- LeetCode[Sort]: Maximum Gap
- LeetCode(164)Maximum Gap
- LeetCode(164) Maximum Gap
- [leetcode 164] Maximum Gap
- LeetCode---(164)Maximum Gap
- LeetCode #164 Maximum Gap
- leetcode 164: Maximum Gap
- LeetCode 164 Maximum Gap
- Leetcode 164 Maximum Gap
- [Leetcode] #164 Maximum Gap
- Leetcode 164 Maximum Gap (最大差距)
- [leetcode-164]Maximum Gap(c)
- [LeetCode]--164. Maximum Gap(Radix Sort && Bucket Sort)
- LeetCode 题解(84): Maximum Gap
- LeetCode 164. Maximum Gap(最大空隙)
- leetcode-Maximum Gap
- bitMap处理工具类
- RxJava 与 Retrofit 结合的最佳实践
- Qt之进程间通信(共享内存)
- 报错 Error: Permission denied to access property "href"
- C++中typename关键字的使用方法和注意事项
- leetcode:sort:Maximum Gap(164)
- 前端小工具
- pycharm——激活码
- 读书笔记-疯狂Java讲义(第三版)第三章 数据类型和运算符
- 2
- MemSQL性能测试结果
- 大概总结了一下jQuery的一些基础知识点
- 代码笔记 | ajax实现服务器与浏览器长连接的功能
- ajax跨域请求