leetcode:sort:Maximum Gap(164)
来源:互联网 发布:下载宇龙汽修仿真软件 编辑:程序博客网 时间:2024/06/05 00:51
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
- Vuejs——(4)v-if、v-for
- maven pom详解
- 有章法的学习c++(0)使用命令行编译文件
- easyUI吧复选框选中的value值返回
- 如何把UIView转成UIImage,解决模糊失真问题
- leetcode:sort:Maximum Gap(164)
- Joda-Time 简介
- 常用的JS小技巧------数组内不规则字符的排序
- c++各容器总结
- word里自动调整图片大小并且添加黑色框线
- Android热修复实践应用--AndFix
- Coordinatlayout学习
- 字符、字节的概念及其区别
- Linux文件属性知识