[LeetCode]164. Maximum Gap

2017–2024年中国10米/20米分辨率水稻种植分布数据集 摘要:本数据集提供2017-2024年中国23个省级行政区的水稻种植分布数据,包含单季稻和双季稻分类(GeoTIFF格式,WGS84坐标系)。5个省份分辨率为20米,其余为10米,基于Sentinel卫星数据和TWDTW方法生成。数据公开共享(7.99GB),使用需注明来源并引用相关文献(DOI:10.12199/nesdc.ecodb.rs.2025.012)。涉及学科包括遥感、生态学和地理学,研究成果需发送至国家生态科学数据中心备案。 阅读详情

原题链接:https://leetcode.com/problems/maximum-gap/

这道题如果是O(nlgn)方法肯定很简单,排序后找最大gap就行了。但如果要求O(n)时间完成的话,难度就大大加大了。所以我们用桶排序的方法来做这道题。 有关桶排序的简介,大家可以参考维基-桶排序。我们来介绍一下桶排序的应用,桶排序就是将一定范围的值全部丢到同一个桶里。所以我们只要合理安排桶的个数,让最大的差异出现的桶与桶之间即可。假设输入的数组中的个数是n,范围从a~b。则我们会发先最大的gap一定会大于等于ceiling(b-a)/(n-1),因为如果数组是等差数列,数与数之间的差异是ceiling(b-a)/(n-1),所以最大gap不可能比这个还小。所以我们假设让桶的容量变成ceiling(b-a)/(n-1),那最大差异一定发生的桶之间。所以我们只要设置(b-a)/[ceiling(b-a)/(n-1)]+1个桶即可。然后就是桶之间的比较我们只需比较上一个桶的最大值和当前桶的最小值即可。所以桶里只需维护一个最大值和一个最小值。时间复杂度是O(n),空间复杂度是O(n)

代码如下:

public class Solution {
    public class bucket
    {
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
    }
    
    public int maximumGap(int[] nums) {
        if(nums.length <= 1)
            return 0;
        int len = nums.length;
        bucket[] buckets = new bucket[len];
        int min = nums[0];
        int max = nums[0];
        for(int i = 0;i<len;i++)
        {
            if(nums[i]<min)
                min = nums[i];
            if(nums[i]>max)
                max = nums[i];
        }
        int seg = (int)Math.ceil((double)(max-min)/(len-1));
        if(seg == 0)
            return 0;
        for(int i = 0;i<len;i++)
        {
            int t = nums[i];
            int k = (t-min)/seg;
            if(buckets[k]==null)
            	buckets[k] = new bucket();
            if(buckets[k].max<t)
                buckets[k].max = t;
            if(buckets[k].min>t)
                buckets[k].min = t;
        }
        int gap = 0;
        int preMax = max;
        for(int i = 0;i<buckets.length;i++)
        {
            if(buckets[i]!=null)
            {
                gap = Math.max(gap,buckets[i].min-preMax);
            	preMax = buckets[i].max; 
            }
        }
        return gap;
    }
}
这道题方法很巧妙,思路也就是想着如何把数据分组开始。如果遇到类似的变种题没思路,面试官提示用到桶排序,大家一定要知道该怎么往下想。

IMSL CNL 7.0.0 (x86-64)Crack IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件 立即下载

相关推荐

2026款拯救者Y9000P深度学习环境配置指南

这套环境适配 2026 款拯救者 Y9000P 的 RTX5060,可直接用于 CV(图像分类、检测)、NLP 等 PyTorch 深度学习任务,GPU 利用率正常,无兼容性问题。后续复现环境时,直接按本文步骤操作即可~

大糊涂虫 2635

164. Maximum Gap [leetcode]

这道题可以有两种刚解法: 解法一: 基于bucket sort的方法: 有n个未排序的数字。 1、先求出数组内最大数max和最小数min。 2、求出每个桶内的数字范围:gap = (max - min) / (n - 1)。 3、算出桶的数量: N = (max - min)/ gap + 1  (可知 N >= n) 每个桶对应的数字范围为:  第一个:[min, min

angie的博客 327

对比学习损失函数避坑指南:为什么你的自监督模型不收敛?

本文深入剖析对比学习损失函数中的关键陷阱,揭示自监督模型不收敛的根本原因。通过分析温度系数选择、负样本采样策略和特征归一化等核心因素,提供实用解决方案。实验数据表明,正确设置温度系数τ(建议0.07起)可提升30%性能,而MoCo风格的负样本队列比SimCLR方案在小批量时准确率高6-8%。

nokia的博客 922

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...

分享人工智能学习心得与实践经验,探讨应用场景,见证变革与进步 763

四、嘉立创EDA原理图绘制实战:从零搭建STM32温湿度监测系统

本文详细介绍了使用嘉立创EDA标准版从零开始绘制STM32温湿度监测系统原理图的完整流程。涵盖核心元器件选型、电源电路设计、ADC按键创新应用及模块化布局技巧,帮助电子爱好者快速掌握STM32系统设计,提升嵌入式开发实战能力。

h0i1j2k3l的博客 883

Nettrace 源码分析

文章介绍:本文将围绕腾讯开源项目nettrace工具中数据采集,数据处理,数据分析三个模块深入源码分析其实现细节

abobob的博客 989

【LeetCode解题报告】164. Maximum Gap

题目来源:https://leetcode.com/problems/maximum-gap/description/题目:Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Return 0 if the array contains les...

Angry_Bird16的博客 334

C++cgal库配置+VS2017

Cgal几何图形库在VS2017上的下载、安装与配置。想要一口气下载完所有安装配件的,可以去文章末尾的链接。httpshttpshttpshttpshttpshttpshttpshttpshttpshttpshttpshttpshttpshttpshttps。

dsn3S的博客 1488

164. Maximum Gap(最大间距)———附带思路和完整代码

文章目录0 效果1 题目2 思路3 代码 0 效果 1 题目 2 思路 朴素算法,先处理特殊情况,然后排序序列,依次比较两个相邻元素的差值并记录最大值。 3 代码 int cmp(const void* p1, const void* p2){ return *(int*)p2 - *(int*)p1; } int maximumGap(int* nums, int numsSize){ if(numsSize < 2) return 0; if(numsSize ==

韩江雪de 小屋 562

Rapid SCADA MQTT

一、配置RapidSCADA 1、准备KpMqtt.dll、Jint.dll库文件,从网盘下载。 2、将以上两个文件拷贝到SCADA安装目录的Comm/KP目录下,如下: C:\SCADA\ScadaComm\KP 3、新建SCADA项目 4、新建Line 5、新建Device 6、拷贝Config目录下的KpMqtt_001.xml文件、KpMqtt_Job.js文件到项目Comm\Config下,如下示例链接 C:\SCADA\Projects\MQTTProject\Instances\Defau

mumanquan1的博客 3005

MIPI RFFE 信号完整性与硬件设计

MIPI RFFE信号完整性设计关键点 摘要:MIPI RFFE作为射频前端控制总线,其信号完整性设计需平衡速率、驱动能力与抗干扰性。核心挑战在于确保接收端在52Mb/s速率下保持足够的电压和时序裕量。关键设计考量包括: 驱动能力优化:过强导致过冲/振铃和射频杂散,过弱则恶化时序裕量,需与总线负载匹配。 波形质量指标:严格控制上升/下降时间(兼顾EMC和时序要求),抑制过冲/振铃幅度,管理VIO电源纹波和耦合噪声。

sinat_15150363的博客 237

LeetCode: 164. Maximum Gap

LeetCode: 164. Maximum Gap 题目描述 Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Return 0 if the array contains less than 2 elements. Exampl...

杨领well的专栏 225

【AUTOSAR-E2E】-2.3-配置E2E:基于E2EPW配置一个RX接收的Signal Group(使用E2E Profile 1)

《共13页精讲》:此法也适用于E2E Profile 4/5/6/7/11/22 目录 1 检查CAN Matrix Arxml/DBC是否满足E2E需求 2 在DaVinci Developer中为Port添加E2E保护 3 在DaVinci Configurator settings设置 4 如何使用应用层的E2E保护 5 结尾 <--返回「Autosar_BSW高阶配置」总目录--> <--返回「Autosar_BSW高阶配置」专栏主页--> <-.

「汽车电子助手」的博客 3949

leetcode-164 Maximum Gap

1.leetcode链接(hard) 2. 题目描述 给定未排序的数组,找到其排序形式中的连续元素之间的最大差异。 如果数组包含少于2个元素,则返回0。 Case example: Input: [3,6,9,1] Output: 3 Explanation: 排序后是[1,3,6,9], (3,6)或(6,9) 有最大差3. 3.思路分析 复杂度分析 ...

公众号/知乎: AI算法札记,欢迎关注交流 338

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Maximum Gap Leetcode #164 题解[Python]

题目来源 https://leetcode.com/problems/maximum-gap/description/ 题意分析 Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Return 0 if the array ...

Wayne_Mai的博客 1052

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

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 eleme

BUILD 4809

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

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

无名山丘,崛起成峰 4241

LeetCode164—Maximum Gap

LeetCode164—Maximum Gap

上善若水 763

[LeetCode164] 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

coder 进阶的专栏 1413

LeetCode 164. Maximum Gap

LeetCode 164. 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

yanqinghe123的专栏 500

Leetcode164 Maximum Gap

leetcode164题

YinRJ的博客 136

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 ma

指尖飞舞 1258

leetcode[164] Maximum Gap

梅西刚梅开二度,我也记一题。 在一个没排序的数组里,找出排序后的相邻数字的最大差值。 要求用线性时间和空间。 如果用nlgn的话,直接排序然后判断就可以了。so easy class Solution { public: int maximumGap(vector<int> &num) { if (num.size() &...

131

Leetcode 164. Maximum Gap

这道题先排序再遍历一遍能够直接AC,但是在讨论区有人提出了使用桶排序的方式,理论上比直接调库排序更加快,但实际上直接调库速度更快。 首先,定义max, min, N分别为数组最大值、最小值、以及数组长度,根据抽屉原理,显然maximum gap ≥ gap = round_up[(max - min) / (N-1)]。反证法如果maximum gap < gap,则会出现 min +...

I_ren的博客 193

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

夜色之浓,莫过于黎明前的黑暗。 243

[leetcode] 164. Maximum Gap

题目链接:https://leetcode.com/problems/maximum-gap/ 题意:给定一个无序数组,求有序情况下相邻元素差最大为多少? 要求:时间复杂度和空间复杂度均为O(n)法一:我们很容易能够想到基数排序,它的时间复杂度和空间复杂度都是O(n),排好序之后扫一遍就行了。这样当然是没有问题的。代码如下:class Solution { public: void rad

曲径通幽处 1091
上一篇: [LeetCode]163. Missing Ranges
foolnote
博客等级 码龄11年 0粉丝 10原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值