Maximum Gap

Given an unsorted array, find the maximum difference between the successive elements in its sorted form.

Try to solve it in linear time/space.

Return 0 if the array contains less than 2 elements.

You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.

 

C++实现代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class Solution {
public:
    int maximumGap(vector<int> &num) {
        if(num.size()<2)
            return 0;
        sort(num.begin(),num.end());
        int i;
        int maximum=0;
        for(i=0;i<num.size()-1;i++)
        {
            if(num[i+1]-num[i]>maximum)
                maximum=num[i+1]-num[i];
        }
        return maximum;
    }
};

int main()
{
    vector<int> vec={23,4,33,5,34,36,97,68,88,90};
    Solution s;
    cout<<s.maximumGap(vec)<<endl;
}

 

时间: 2024-10-11 04:59:35

Maximum Gap的相关文章

[LeetCode]164.Maximum Gap

题目 Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements. You may assume all elements in the array are non-ne

[LeetCode] Maximum Gap

Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements. You may assume all elements in the array are non-negat

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

0206关于Dg Gap Detection and Resolution2

[20170206]关于Data Guard Gap Detection and Resolution2.txt --测试看看一些参数不设置是否可以解决gap问题. 1.环境: SYS@book> @ &r/ver1 PORT_STRING                    VERSION        BANNER ------------------------------ -------------- ----------------------------------------

0206关于DG Gap Detection and Resolution3

[20170206]关于Data Guard Gap Detection and Resolution3.txt --测试看看一些参数不设置是否可以解决gap问题.相关链接: http://blog.itpub.net/267265/viewspace-2133106/ http://blog.itpub.net/267265/viewspace-2133107/ 1.环境: SYS@book> @ &r/ver1 PORT_STRING                    VERSION

the terminal server has exceeded the maximum number of allowed connections

the terminal server has exceeded the maximum number of allowed connections 参考解决方法: http://dog.xmu.edu.cn/2007/07/22/mstsc-3389/   电脑用太久了,最近一直无缘无故重起,重起就会导致连接到的远程终端断开,再也无法再连接,会提示the terminal server has exceeded the maximum number of allowed connections

PHP中超时提示Fatal error: Maximum execution time of 30 seconds

由于近日做的程序中涉及到的循环比较多且处理的情况较复杂,所以在运行程序时出现执行超时提示如下: Fatal error: Maximum execution time of 30 seconds exceeded in D:\php\AppServ\www\sum3\test.php on line 3 通过在网上搜索,找到以下解决方案给大家分享一下: 这个错误是说你的php 执行时间越过了配置文件中设置的最大执行时间30秒钟,这不是你的程序本身存在的问题,而 是系统的配置文件问题,如果你的网速

Geeks 面试题之Maximum size square sub-matrix with all 1s

Maximum size square sub-matrix with all 1s Given a binary matrix, find out the maximum size square sub-matrix with all 1s. For example, consider the below binary matrix. 0 1 1 0 1 1 1 0 1 0 0 1 1 1 0 1 1 1 1 0 1 1 1 1 1 0 0 0 0 0 The maximum square s

HDU 1839 Delay Constrained Maximum Capacity Path(二分+最短路)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1839 题目: Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 226    Accepted Submission(s): 98 Problem Descri